03 DS and Algorithm Session 04
03 DS and Algorithm Session 04
Sorting Data
Suppose, you have to invite your friends and relatives for your birthday party. For this, you need to give them a call. You are looking for the telephone number of a friend named Steve in a telephone directory with 10,000 records. However, the records are stored randomly and not in any particular order.
To search for the telephone number of your friend in such a directory, you would need to scan each entry in the directory in a sequential manner. This would be very time consuming. How can you solve this problem?
A simple solution to save time, and search records efficiently is sorting. Sorting is the process of arranging data in some pre-defined order or sequence. The order can be either ascending or descending. If the data is ordered, you can directly go to the section that stores the names starting with S, thereby reducing the number of records to be traversed.
To understand the implementation of bubble sort algorithm, consider an unsorted list of numbers stored in an array.
0
arr
1 2
2 6
3 7
4 3
0
arr
1 2
2 6
3 7
4 3
Pass 1 n=5 Compare the element stored at index 0 with the element stored at index 1.
0
arr
1 2
2 6
3 7
4 3
Pass 1 n=5 Swap the values if they are not in the correct order.
Swap 0
arr
1 5 2
2 6
3 7
4 3
2 5
Pass 1 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2.
No Change 0
arr
1 5
2 6
3 7
4 3
Pass 1 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3.
No Change 0
arr
1 5
2 6
3 7
4 3
Pass 1 n=5 Compare the element stored at index 3 with the element stored at index 4 and swap the values if the value at index 3 is greater than the value at index 4.
Swap 0
arr
1 5
2 6
3 3 7
4 7 3
Pass 1 n=5 Compare the element stored at index 3 with the element stored at index 4 and swap the values if the value at index 3 is greater than the value at index 4.
0
arr
1 5
2 6
3 3
Pass 2 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1.
No Change 0
arr
1 5
2 6
3 3
Pass 2 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2.
No Change 0
arr
1 5
2 6
3 3
Pass 2 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3.
Swap 0
arr
1 5
2 3 6
3 6 3
Pass 2 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3.
0
arr
1 5
2 3
Pass 3 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1.
No Change 0
arr
1 5
2 3
Pass 3 n=5 Compare the element stored at index 1 with the element stored at index 2 and swap the values if the value at index 1 is greater than the value at index 2.
Swap 0
arr
1 5 3
2 5 3
Pass 3 n=5 Compare the element stored at index 2 with the element stored at index 3 and swap the values if the value at index 2 is greater than the value at index 3.
0
arr
1 3
Pass 4 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1.
No Change 0
arr
1 3
Pass 4 n=5 Compare the element stored at index 0 with the element stored at index 1 and swap the values if the value at index 0 is greater than the value at index 1.
0
arr
3 5
0
arr
3 5
void bubble_sort(int array[], int size) { int temp, pass, i; for (pass = 0; pass < size-1; pass++) for (i = 0; i < size-pass-1; i++) if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } }
void main(void) { int array[30], i,size; clrscr(); printf("enter the size of the Array:\n"); scanf("%d",&size); printf("Enter the values::\n"); for (i = 0; i < size; i++) scanf("%d",&array[i]); printf("\n Unsorted list is as follows\n"); for (i = 0; i < size; i++) printf(" %d", array[i]); bubble_sort(array, size); printf("\n Sorted list is as follows\n"); for (i = 0; i < size; i++) printf("%d ", array[i]); getch(); }
The efficiency of a sorting algorithm is measured in terms of number of comparisons. In bubble sort, there are n 1 comparisons in Pass 1, n 2 comparisons in Pass 2, and so on. Total number of comparisons = (n 1) + (n 2) + (n 3) + + 3 + 2 + 1 = n(n 1)/2. n(n 1)/2 is of O(n2) order. Therefore, the bubble sort algorithm is of the order O(n2).
Just a minute
Answer:
The bubble sort algorithm has a quadratic order of growth.
Just a minute
While implementing bubble sort algorithm, how many comparisons will be performed in Pass 1.
Answer:
n 1 comparisons
To understand the implementation of selection sort algorithm, consider an unsorted list of numbers stored in an array.
0
arr
105 120 10
200 20
0
arr
105 120 10
200 20
Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n 1].
0
arr
105 120 10
200 20
min
Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n 1]. Swap the minimum value with the value at index 0.
Swap 0
arr
min
Pass 1 n=5 Search the minimum value in the array, arr[0] to arr[n 1]. Swap the minimum value with the value at index 0.
0
arr
Pass 2 n=5 Search the minimum value in the array, arr[1] to arr[n 1]. Swap the minimum value with the value at index 1.
Swap 0
arr
min
Pass 2 n=5 Search the minimum value in the array, arr[1] to arr[n 1]. Swap the minimum value with the value at index 1.
0
arr
The second smallest value is placed at its correct location after Pass 2
Pass 3 n=5 Search the minimum value in the array, arr[2] to arr[n 1]. Swap the minimum value with the value at index 2.
0
arr
min
Pass 3 n=5 Search the minimum value in the array, arr[2] to arr[n 1]. Swap the minimum value with the value at index 2.
0
arr
min The third smallest value is placed at its correct location after Pass 3
Pass 4 n=5 Search the minimum value in the array, arr[3] to arr[n 1]. Swap the minimum value with the value at index 3.
Swap 0
arr
min
Pass 4 n=5 Search the minimum value in the array, arr[3] to arr[n 1]. Swap the minimum value with the value at index 3.
0
arr
The fourth smallest value is placed at its correct location after Pass 4
0
arr
void selection_sort(int array[], int size) { int temp, i, j,min_index; for (i = 0; i < size-1; i++) { min_index=i; for(j=i+1;j<size;j++) { if(array[j]<array[min_index]) min_index=j; } temp=array[i]; //swap(array[i],array[min_index]); array[i]=array[min_index]; array[min_index]=temp; } }
Determining the Efficiency of Selection Sort Algorithm In selection sort, there are n 1 comparisons during Pass 1 to find the smallest element, n 2 comparisons during Pass 2 to find the second smallest element, and so on. Total number of comparisons = (n 1) + (n 2) + (n 3) + + 3 + 2 + 1 = n(n 1)/2 n(n 1)/2 is of O(n2) order. Therefore, the selection sort algorithm is of the order O(n2).
Just a minute
Answer:
True
Just a minute
How many comparisons are performed in the second pass of the selection sort algorithm?
Answer:
n2
To understand the implementation of insertion sort algorithm, consider an unsorted list of numbers stored in an array.
0
arr
3 10
4 20
70
80 30
0
arr
3 10
4 20
70
80 30
80 30
Sorted List
Unsorted List
Pass 1 Place the first element from the unsorted list at its correct position in the sorted list.
0 70
3 10
4 20
80 30
Sorted List
Unsorted List
Pass 1 Place the first element from the unsorted list at its correct position in the sorted list.
0 70
1 80
2 30
10 20
Sorted List
Unsorted List
Pass 2 Place the first element from the unsorted list at its correct position in the sorted list.
0 70
1 80
2 30
10 20
Sorted List
Unsorted List
Pass 2 Place the first element from the unsorted list at its correct position in the sorted list.
30
70 80
10 20
Sorted List
Unsorted List
Pass 3 Place the first element from the unsorted list at its correct position in the sorted list.
30
70 80
10 20
Sorted List
Unsorted List
Pass 3 Place the first element from the unsorted list at its correct position in the sorted list.
1 30
4 20
10
70 80
Sorted List
Unsorted List
Pass 4 Place the first element from the unsorted list at its correct position in the sorted list.
1 30
4 20
10
70 80
Sorted List
Unsorted List
Pass 4 Place the first element from the unsorted list at its correct position in the sorted list.
0 10
3 70
4 80
20 30
Sorted List
Unsorted List
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =1
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =1
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =1 temp = 80
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =1 temp = 80
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =1
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2 temp = 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2 temp = 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2 temp = 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30
n=5 i =2 temp = 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
80 30 80
n=5 i =2 temp = 30
2. 3. 4.
0
arr
2 80
3 10
4 20
5.
70
n=5 i =2 temp = 30
2. 3. 4.
0
arr
3 10
4 20
5.
70
70 80
n=5 i =2 temp = 30 j = 1
2. 3. 4.
0
arr
3 10
4 20
5.
70 80
n=5 i =2 temp = 30 j = 1
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =2
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =3
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =3 temp = 10
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =3 temp = 10
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =3 temp = 10
2. 3. 4.
0
arr
3 10
4 20
5.
30
70 80
n=5 i =3 temp = 10
2. 3. 4.
0
arr
3 80 10
4 20
5.
30
70 80
n=5 i =3 temp = 10
2. 3. 4.
0
arr
1 70
3 80
4 20
5.
30
n=5 i =3 temp = 10
2. 3. 4.
0
arr
1 70
2 70
3 80
4 20
5.
30
n=5 i =3 temp = 10
2. 3. 4.
0
arr
2 70
3 80
4 20
5.
30
n=5 i =3 temp = 10
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
30
n=5 i =3 temp = 10 j = 1
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
n=5 i =3 temp = 10 j = 1
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =3
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =4
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
3 80
4 20
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
3 80
4 80 20
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
4 80
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 70
3 70
4 80
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
3 70
4 80
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 30
2 30
3 70
4 80
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
2 30
3 70
4 80
5.
10
n=5 i =4 temp = 20
2. 3. 4.
0
arr
1 20
2 30
3 70
4 80
5.
10
n=5 i =4
2. 3. 4.
0
arr
1 20
2 30
3 70
4 80
5.
10
j
The list is now sorted
void insertion_sort (int array[], int n) { int temp, i, j; for (i = 1 ; i < n; i++) { temp = array [i]; j=i-1; while (j>=0 && array[j]>temp) { array[j+1]=array [j]; j--; } array[j+1]=temp;
To sort a list of size n by using insertion sort, you need to perform (n 1) passes. Best Case Efficiency:
Best case occurs when the list is already sorted. In this case, you will have to make only one comparison in each pass. In n 1 passes, you will need to make n 1 comparisons. The best case efficiency of insertion sort is of the order O(n).
Just a minute
A sales manager has to do a research on best seller cold drinks in the market for the year 2004-2006. David, the software developer, has a list of all the cold drink brands along with their sales figures stored in a file. David has to provide the sorted data to the sales manager. The data in the file is more or less sorted. Which sorting algorithm will be most efficient for sorting this data and why?
Answer:
Insertion sort provides better efficiency than bubble sort and selection sort when the list is partially sorted. Therefore, David should use the insertion sort algorithm.
To understand the implementation of shell sort algorithm, consider an unsorted list of numbers stored in an array.
0
arr
3 10
4 80
5 20
6 90
7 110
8 75
9 60
10 45
70
30 40
0
arr
3 10
4 80
5 20
6 90
7 110
8 75
9 60
10 45
70
30 40
60
80 110 45
2 List 3 = 0
arr
40 1
20 75 2 3 10 4 80 5 20 6 90 7 110 8 75 9 60 10 45
70
30 40
60 70 10 90
80 110 45 45 80 110
2 List 3 = 20 40
40 75 20
Apply insertion sort to sort the The lists are sorted three lists
60 70
2 List 3 = 0
arr
20 2
40 75 3 60 4 45 5 40 6 70 7 80 8 75 9 10
10
30 20
90 110
75 110
1 List 2 = 0
arr
7 80 5 40
9 90 6 70 7 80 8 75 9 10
30 2
60 40 3 60 4 45
10
30 20
90 110
10
20 45
75 110
1 List 2 = 30
7 80
9 90
60 40
10
20 45
75 110
1 List 2 = 30
7 80
9 90
40 60
10
20 45
75 110
1 List 2 = 0
arr
7 80 5 60
9 90 6 70 7 80 8 75 9 10
30 2
40 60 3 40 4 45
10
30 20
90 110
Increment = 1 Pass = 3
0
arr
3 40
4 45
5 60
6 70
7 80
8 75
10
10
30 20
90 110
Increment = 1 Pass = 3
0
arr
3 40
4 45
5 60
6 70
7 75
8 80
10
10
20 30
90 110
Just a minute
Which of the following sorting algorithms compares the elements separated by a distance of several positions to sort the data? The options are:
1. 2. 3. 4. Insertion sort Selection sort Bubble sort Shell sort
Answer:
4. Shell sort
Summary
Summary (Contd.)
Bubble sort and selection algorithms have a quadratic order of growth, and are therefore suitable for sorting small lists only. Insertion sort performs different number of comparisons depending on the initial ordering of elements. When the elements are already in the sorted order, insertion sort needs to make very few comparisons. Insertion sort is an efficient algorithm than bubble sort and selection sort if the list that needs to be sorted is nearly sorted.
Summary (Contd.)
Shell sort improves insertion sort by comparing the elements separated by a distance of several positions. This helps an element to take a bigger step towards its correct position, thereby reducing the number of comparisons.