0% found this document useful (0 votes)
33 views

Searching & Sorting Introduction To Sorting

The document discusses various sorting algorithms including selection sort, bubble sort, and insertion sort. Selection sort works by selecting the minimum value and swapping it with the first element, repeating until sorted. Bubble sort works by comparing adjacent elements and swapping them if out of order, repeating until sorted. Insertion sort works by picking a value and inserting it into the correct place in the partially sorted list. The time complexity of selection and bubble sort is O(n^2) while insertion sort is more efficient for small data sets.

Uploaded by

Swayam Dixhit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Searching & Sorting Introduction To Sorting

The document discusses various sorting algorithms including selection sort, bubble sort, and insertion sort. Selection sort works by selecting the minimum value and swapping it with the first element, repeating until sorted. Bubble sort works by comparing adjacent elements and swapping them if out of order, repeating until sorted. Insertion sort works by picking a value and inserting it into the correct place in the partially sorted list. The time complexity of selection and bubble sort is O(n^2) while insertion sort is more efficient for small data sets.

Uploaded by

Swayam Dixhit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SORTING P a g e | 6.

SEARCHING & SORTING


Introduction to sorting 13. for(i=0;i<5;i++)
14. for(j=i+1;j<5;j++)
 Arranging the data in a specific order is known as sorting. This logical
15. if(arr[i] > arr[j])
order may be ascending or descending.
 In ascending order, the minimum value is placed on top and the 16. {
maximum value is placed in the bottom. 17. temp = arr[i];
 The descending order is just reverse with the maximum value placed on 18. arr[i] = arr[j];
top and the minimum at the bottom. 19. arr[j] = temp;
 Sorting can be classified into two classes – internal sorting and external 20. }
sorting.
21. printf("The sorted array : \n");
 Internal sorting means that we are working with only those values that are
22. for(i=0;i<5;i++)
stored in the computer’s primary memory.
23. printf("%d\t",arr[i]);
 If the elements to be sorted are stored in files (secondary storage), it is said
to be external sorting. 24. getch();
25. }
Types of sorting
Algorithm to sort the values using Selection sort technique.
 There are so many types of sorting. Some of them are discussed below :
Note : arr is an array of 5 integer values
Selection sort Step 1 : Input 5 values in array arr
 In this process, the minimum value is selected and is swapped with the Step 2 : Set i = 0
first element of the array. Step 3 : Repeat Step 4 to Step 11 while (i<5)
Step 4 : Set j = i + 1
Program : WAP to sort an integer array using Selection sort technique.
Step 5 : Repeat Step 6 to Step 10 while (j<5)
1. #include<stdio.h>
Step 6 : if (i[arr] > j[arr])
2. #include<conio.h>
Then go to Step 7
3. main()
Else go to Step 10
4. {
5. int arr[5],i,j,temp;
Step 7 : temp = i[arr]
6. clrscr(); Step 8 : i[arr] = j[arr]
7. printf("Enter 5 integers : \n"); Step 9 : j[arr] = temp
8. for(i=0;i<5;i++) Step 10 : Increment j by 1
9. { Step 11 : Increment i by1
10. printf("\tEnter a value : "); Step 12 : Print the values of array arr
11. scanf("%d", &arr[i]); Step 13 : Stop
12. }

ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.2

 The number of comparisons is independent of the original order of the 8. for(i=1;i<5;i++)


elements. 9. for(j=0;j<5-i;j++)
10. if(arr[j] > arr[j+1])
 To find the smallest element, there are n-1 comparisons in the first pass, n-
11. {
2 comparisons in the second pass and so on. Therefore, n-i comparisons
12. temp = arr[j];
are made in the ith pass.
13. arr[j] = arr[j+1];
 Hence, total number of comparisons are – 14. arr[j+1] = temp;
f(n) = (n-1) + (n-2) + (n-3) + …………… + (n-i) + ……….. + 3 + 2 + 1 15. }
= n × (n-1)/2 16. printf("\nThe sorted array is : \n");
= O(n2) 17. for(i=0;i<5;i++)
 That is the time required to execute selection sort algorithm is 18. printf("%d\t", arr[i]);
proportional to n2 where n is the number of input items. 19. }
 Selection sort will be useful where n is very small. There is not much
Algorithm to sort the values using Bubble sort technique.
difference between the worst time and best time in selection sort.
Note : arr is an array of 5 integer values
Bubble sort Step 1 : Input 5 values in array arr
 Bubble sorting is one of the most popular sorting techniques. However, Step 2 : Set I = 1
when the data is in huge volumes, time taken to sort the data also Step 3 : Repeat Step 4 to Step 11 while (i<5)
increases. Step 4 : Set j = 0
 This is also known as exchange sort technique. Step 5 : Repeat Step 6 to Step 10 while (j<5-i)
 In this technique, if the values are to be arranged in ascending order, the Step 6 : if (j[arr] > j+1[arr])
maximum value is first moved to the last element position in the array. Then go to Step 7
 To go through this process, only two successive values are always Else go to Step 10
compared. Step 7 : temp = j[arr]
 There are n-1 comparisons during the first pass which places the largest Step 8 : j[arr] = j+1[arr]
element in the last position. Step 9 : j+1[arr] = temp
 There are n-2 comparisons in the second pass, which places the second Step 10 : Increment j by 1
largest element at its destination and so on. Step 11 : Increment I by1
Step 12 : Print the values of array arr
Program : WAP to sort an integer array using bubble sort technique. Step 13 : Stop
1. #include<stdio.h>
2. main()  Total number of comparisons are –
3. { f(n) = (n-1) + (n-2) + (n-3) + …………… + (n-i) + ……….. + 3 + 2 + 1
4. int arr[5], i,j,temp; = n × (n-1)/2
5. printf("Enter 5 integers : \n"); = n2/2 – n/2
6. for(i=0;i<5;i++) = O(n2)
7. scanf("%d", &arr[i]);

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

Quick Sort 10. for (i=0 ; i<5 ; i++)


11. printf ("\t%d", a[i]);
 The idea behind this sorting is that sorting is much easier in two short lists
12. }
rather than one long list.
13. void qsort (int lb, int ub)
 It is based on divide – and – conquer mechansim.
14. {
 This means that divide the big problem into two small problems and then
15. int i, j, key, temp;
those two small problems into two small ones and so on.
16. int flag = 1;
 In this technique, we divide the original list into two sublists.
17. if (lb<ub)
 We choose a key value from which all the left side of elements are smaller
18. {
and all the right side of elements are greater than that element.
19. i=lb;
arr
20. j=ub;
Partition 1 Partition 2 21. key=a [lb];
values < key key values > key 22. while (flag)
Algorithm to sort the elements using quick sort 23. {
24. i++;
Step 1 : Take the first element of the array as pivot.
25. while (a[i]<key)
Step 2 : Compare the pivot element one by one from right to left for getting
26. i++;
the element which has value less the pivot.
27. while (a[j]>key)
Step 3 : Interchange the element with pivot.
28. j--;
Step 4 : Now the comparison will start from the interchanged element
29. if (i<j)
position from left to right for getting the element higher than pivot.
30. {
Step 5 : Repeat Step 2 to Step 4 until pivot is at its proper position.
31. temp=a [i];
Step 6 : Create the sublists at left and right side of pivot.
32. a [i]=a [j];
Step 7 : Repeat the same process until all elements of lists are at proper
33. a [j]=temp;
position in list.
34. }
PROGRAM TO SORT VALUES USING QUICK SORT 35. else
1. #include<stdio.h> 36. flag = 0;
2. int a[5]; 37. }
3. main () 38. temp=a [lb];
4. { 39. a [lb] = a [j];
5. int i; 40. a [j] = temp;
6. for(i=0 ; i<5 ; i++) 41. qsort (lb, j-1);
7. scanf ("%d", &a[i]); 42. qsort (j+1, ub);
8. qsort (0,4); 43. }
9. printf ("The sorted array is as follows : \n"); 44. }

ACC, 103/9, Sec. – 10, Kumbha Marg, Pratap Nagar, Sanganer, JAIPUR. Contact Nos. : 77370-08028, 98285-48028
SORTING P a g e | 6.7

Merge sort 20. printf("\n");


21. getch();
 This sorting technique is also based upon divide-and-conquer mechanism.
22. }
 In merge sort, we divide the array into subarrays of size 2 and merge
23. void merge_sort(int low, int high)
adjacent pairs.
24. {
 The process of merge sort is shown in the following figures –
25. int mid;
15 90 50 25 100 6 2 10 75 60 29 13
26. if(low!=high)
15 90 25 50 6 100 2 10 60 75 13 29 27. {
28. mid=(low+high)/2;
15 25 50 90 2 6 10 100 13 29 60 75 29. merge_sort (low, mid) ;
30. merge_sort(mid+1, high) ;
2 6 10 15 25 50 90 100 13 29 60 75 31. merge(low, mid, high);
32. }
2 6 10 13 15 25 29 50 60 75 90 100 33. }
34.
PROGRAM TO SORT VALUES USING MERGE SORT TECHNIQUE
35. void merge(int low, int mid, int high)
1. #include<stdio.h>
36. {
2. # define MAX 5
37. int temp[MAX];
3. int array[MAX];
38. int i=low;
4. main()
39. int j=mid +1;
5. {
40. int k = low;
6. int i;
41. while((i<=mid) && (j<=high))
7. clrscr();
42. {
8. for(i=0;i<MAX;i++)
43. if(array[i] < array[j])
9. {
44. temp[k++] = array [i++];
10. printf("Enter element %d:", i+1);
45. else
11. scanf("%d", &array[i]);
46. temp[k++] = array[j++];
12. }
47. }
13. printf("Unsorted list is :\n");
48. while(i<=mid)
14. for(i=0;i<MAX;i++)
49. temp[k++]=array[i++];
15. printf("%d\t", array[i]);
50. while(j<=high)
16. merge_sort(0, MAX-1);
51. temp[k++] = array[j++];
17. printf("\nSorted list is :\n");
52. for(i=low;i<=high;i++)
18. for(i=0;i<MAX;i++)
53. array[i]=temp[i];
19. printf("%d\t", array[i]);
54. }

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

You might also like