Searching & Sorting Introduction To Sorting
Searching & Sorting Introduction To Sorting
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.2
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.3
Insertion sort Algorithm to sort the values using Insertion sort technique.
In the insertion sort technique, we pick up a particular value and then Note : arr is an array of 5 integer values
insert it at the appropriate place in the sorted sub list. Step 1 : Input 5 values in array arr
Program : WAP to sort an integer array using Insertion Sort. Step 2 : Set i = 0
1. #include<stdio.h> Step 3 : Repeat Step 4 to Step 14 while (i < 5)
2. main()
Step 4 : temp = i[arr]
3. {
Step 5 : Set j = 0
4. int arr[5], i, j, k, temp;
5. clrscr(); Step 5 : Repeat Step 6 to Step 13 while (j < i)
6. printf("Enter 5 numbers : \n"); Step 6 : if (temp < j[arr])
7. for(i=0;i<5;i++) Then go to Step 7
8. {
Else go to Step 13
9. printf("\tEnter a value : ");
10. scanf("%d", &arr[i]);
Step 7 : Set k = i
11. } Step 8 : Repeat Step 9 to Step 10 while (k>=j)
12. for(i=1;i<5;i++) /* Number of passes */ Step 9 : k[arr] = k-1[arr]
13. { Step 10 : Decrement k by 1
14. temp = arr[i];
Step 11 : j[arr] = temp
15. for(j=0;j<i;j++)
16. { Step 12 : Go to Step 14
17. if(temp<arr[j]) Step 13 : Increment j by 1
18. { Step 14 : Increment i by 1
19. for(k=i;k>=j;k--)
Step 15 : Print the values of array arr
20. arr[k] = arr[k-1]; /* Swap the values */
Step 16 : Stop
21. arr[j] = temp;
22. break; Radix Sort
23. }
Bucket or Radix sort is a method that can be used to sort a list of names
24. }
alphabetically.
25. }
Here the base or radix is 26.
26. printf("\nNow the sorted array is : \n");
To sort decimal numbers, where radix or base is 10, we need ten buckets.
27. for(i=0;i<5;i++)
These buckets are numbered 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
28. printf("%d\t", arr[i]);
The process of sorting following values is shown in the series of figures –
29. getch();
30. } 2349, 2965, 0345, 0900, 4973, 5456, 6767, 8898, 9050, 7351, 4942, 5433
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.4
Step 1 : Let us arrange the given values on the basis of their unit’s digits As the number of digits in the maximum value (9050) is 4, so we needed
(least significant digit) in the buckets. only 4 passes to sort the given values.
900 7351 4942 4973 2965 5456 6767 8898 2349 After collecting the values once again from the buckets, we get the sorted
9050 5433 345 values.
345, 900, 2349, 2965, 4942, 4973, 5433, 5456, 6767,
7351, 8898, 9050
0 1 2 3 4 5 6 7 8 9 Heap Sort
Step 2 : Now we collect all these values in sequence from bucket 0 to
The elements of the heap tree are represented by an array.
bucket 9 on the FIFO basis.
The root will be the largest element of the hep tree.
Then we arrange these collected values on the basis of tens’s digits.
Since it is maintained in the array, so the largest value should be the last
900 5433 4942 9050 2965 4973 8898 element of the array.
345 2349 7351 6767 For heap sorting, we keep on deleting the root till there is only one
5456 element in the tree.
Then the array which represented the heap tree will now contain sorted
elements.
0 1 2 3 4 5 6 7 8 9
Step 3 : We repeat the process of collecting the values in sequence from Algorithm to sort values using max heap
bucket 0 to bucket 9.
Step 1 : Remove the root node and store it in last empty space of the
This time we arrange these collected values on the basis of hundred’s
sorted array.
digits.
Step 2 : Replace the root node of the tree by the last node value.
9050 345 5433 6767 8898 900 Step 3 : Remove the last node from the max heap.
2349 5456 4942 Step 4 : if the newly placed node is less than its both the children, then
7351 2965 interchange it with the greater value child.
Step 5 : Repeat Step 4 until the tree maintains the max heap property.
4973
0 1 2 3 4 5 6 7 8 9 Let us consider the following max heap to be sorted.
Step 4 : Now we collect the values and place these values again on the
25
basis of their thousand’s digits.
345 2349 4942 5433 6767 7351 8898 9050
900 2965 4973 5456 15 17
6 10 2 11
0 1 2 3 4 5 6 7 8 9
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.5
Delete 25 and place it in the last place of the array. Also replace the root Delete 15
with the last element of the max heap tree. 15 17 25
11 0 1 2 3 4 5 6
2 11
15 17 25
0 1 2 3 4 5 6 Swap
10 11 2 with 11 10 2
6 10 2
Here left and right child of 11 are 15 and 17. Both are greater than 11, but
6 6
right child 17 is greater than left child 15.
Hence, replace it with 17. Now, the max heap tree looks as follows.
17
Delete 11
11 15 17 25
15 11 0 1 2 3 4 5 6
6 10
Swap
6 with 10
6 10 2
10 2 6 2
Now this process of deleting the root node and placing it in the last empty
space of the array is continued till the the tree has at least one node in it. Delete 10
Delete 17 2
Swap
6
17 25 2 with 6
10 11 15 17 25
6 2
0 1 2 3 4 5 6 0 1 2 3 4 5 6
2 15
Delete 6
Swap 6 10 11 15 17 25
15 11 2 with 15 2 11
0 1 2 3 4 5 6
Delete 2
6 10 15 6 10 2 6 10 11 15 17 25
0 1 2 3 4 5 6
Swap So, the final sorted array is –
10 11 2 with 10
2 6 10 11 15 17 25
6 2
0 1 2 3 4 5 6
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.6
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.7
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.8
Sorting Worst case Average case Best case Algorithm to search value using Binary Search technique.
Selection sort O(n2) O(n2) O(n2) Note : arr is an array of 10 integers in sorted order
Step 1 : Set found = 0
Bubble sort O(n2) O(n2) O(n2) Step 2 : Input 10 integer values in the array arr
Insertion sort O(n2) O(n2) O(n2) Step 3 : Input a value in val
Step 4 : low = 0
Merge sort O(nlogn) O(nlogn) O(nlogn)
Step 5 : high = 9
Quick sort O(n2) O(nlogn) O(nlogn) Step 6 : Repeat Step 7 to Step 13 while (low<=high)
Step 7 : mid = (low + high) / 2
Heap sort O(nlogn) O(nlogn) O(nlogn)
Step 8 : if (mid[arr] = val)
Radix sort O(n2) (n2) (n2) Then go to Step 9
Else go to Step 11
Step 9 : found = 1
Algorithm to search value using Linear Search technique.
Step 10 : go to Step 14
Note : arr is an array of 10 integers Step 11 : if (val < arr [mid])
Step 1 : Set found = 0 Then go to Step 12
Step 2 : Input 10 integer values in the array arr Else go to Step 13
Step 3 : Input a value in val Step 12 : high = mid - 1
Step 4 : i=1 Step 13 : low = mid + 1
Step 5 : Repeat Step 6 to Sep 9 while (i < 10) Step 14 : if (found = 1)
Step 6 : if (val = i[arr]) Then go to Step 15
Then go to Step 7 Else go to Step 17
Else go to Step 9 Step 15 : Print “Value is present…”
Step 7 : found = 1 Step 16 : Go to Step 18
Step 8 : Go to Step 10 Step 17 : Print “Value is not present…”
Step 9 : Increment i by 1 Step 18 : Stop
Step 10 : if (found = 1)
Then go to Step 11
Else go to Step 13
Step 11 : Print “Value is present…”
Step 12 : Go to Step 14
Step 13 : Print “Value is not present…”
Step 14 : Stop
ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028