Skip to main content

Posts

Showing posts from April, 2018

Bubble Sort and Selection Sort in C

Sorting is nothing but arranging the data in ascending or descending order. The term sorting came into picture, as humans realized the importance of searching quickly. sorting allows everyone to arrange data in an order, hence making it easier to search. Bubble Sort Algorithm Bubble Sort is a simple algorithm which is used to sort a given set of n elements. Bubble Sort compares all the element one by one and sort them based on their values. Watch the video for the demo  https://www.youtube.com/watch?v=yIQuKSwPlro If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on. If we have total n elements, then we need to repeat this process for n-1 times. But it is noted that when the array is sorted , comparing adjacent element does

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present or not. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search. Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search or linear sear

Command-line Arguments in C

Command-line arguments are a way to pass information or inputs to a C program when you run it from the command line or terminal. These arguments provide additional flexibility and let users control the program's behavior without changing the code. In C, command-line arguments are handled through the  main  function, which takes two parameters: int main(int argc, char *argv[]) Here’s what these parameters mean: argc  (Argument Count): This integer represents the number of command-line arguments, including the program name. argv  (Argument Vector): This array of strings ( char *argv[] ) holds each command-line argument as a string.  argv[0]  is always the program's name, and the remaining elements are the actual arguments passed by the user. Example: Printing Command-Line Arguments Let’s start with a basic example where we simply print all command-line arguments. #include <stdio.h> int main(int argc, char *argv[]) {     printf("Number of arguments: %d\n", argc);