Searching and sorting Algorithm in C



Searching is a process of finding a particular record within a huge amount of data.
Data can be arrays,linked list,tress,etc.

There are 2 categories of searching. They are:
1.Sequential searching (linear search)
2.Interval searching (binary search)


Linear searching

  • It is a type of sequential searching algorithm.
  • Every element within the input array is traversed and compared with the item to be found . If the item matches in search,then it is successfull.
  • The best case complexity is O(1) and the worst case complexity is O(n).
Example:




Binary Searching

  • It is a searching algorithm used in sorted array by repeatedly dividing the search interval in half.
  • It is very efficient.
  • We compare the element with middle element of array

             mid=(1st term) +(last term)/2
         if the element is less than the middle element then we search it in part of the list till middle                     otherwise in the right side of the middle element. The searching process will continue until we               find the element has no left or right part to search.

  • Conditions : Element must be present in sorted list.
  • Algorithm
       4 6 9 10 12 15 16 20 25

       Mid = [ 1 + 9 ]/2 = 8.5 =8

      case1: equal to mid
      case2: greater than mid => move right side of mid
      case3: smaller than mid => move left side of mid
  • Example


Selection sort
  • In selection sort the smallest value among the unsorted element of the array is selected in every pass and inserted to its appropriate position into the array.

  • In this algorithm the array is divided into 2 parts , 1st sorted part and another one is unsorted part. Initially sorted part is empty and another one is unsorted part which is the given array. Sorted part is placed at left while the unsorted part is placed at right.
  • Example:


Bubble sort

  • It is also known as exchange sort. Simplest sorting method.
  • If the array contains N elements then N-1 iterations or passes are required to sort the array.
  • In  each pass two adjacent no. are compared , if they are out of order then swap those no.
  • At the end of 1st iteration the highest value is placed at last position.
  • At the end of 2nd pass, next highest no. is placed at 2nd last position and so on. 
  • It is very efficient sorting algorithm.
  • Example


Insertion sort

  • This algorithm is similar to game of cards. It is assumed that the first card is already sorted in the game and then we select an unsorted card. If the selected unsorted card is greater than 1st card, it will be placed at the right side , otherwise to the left side . Similarly all unsorted card are put in perfect place.
  • Example


Quick sort

  • One of the best sorting algorithm which follows divide and conquer rule.
  • The basic idea behind Quicksort is to select a pivot element from the array and partition the other dements into 2 sub arrays according to whether they are of greater than or less than pivot.
  • This algorithm basically requires implementation of 2 function
  • Partition () function and Quick sort () function
  • Example


Merge sort

  • Merge sort is the most popular sorting algorithm that is based on principle of divide and conquer algorithm.
  • A problem here is divided into multiple sub problems. Each problems is solved individually. Finally , sub problems are combined to form the final solution.
  • The sub lists are divided again into halves until the list cannot be further divided.
  • Example


Questions 

1.  List out any 3 differences between structure and Unions?

Structures:
  •  In structures, each member has its own memory location.
  •  The size of a structure is the sum of the sizes of its members plus any padding.
  • Structures are used to represent a collection of different data types. 
Unions:
  • In unions, all members share the same memory location.
  • The size of a union is the size of its largest member. 
  • Unions are used when different data types need to be stored in the same memory location, and only one value needs to be accessed at a time. 
2. 

Popular Posts