sorting technique - 1
TRANSCRIPT
![Page 1: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/1.jpg)
Different types of Sorting Techniques used in Data
Structures
C#ODE Studio
![Page 2: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/2.jpg)
Sorting: Definition
Sorting: an operation that segregates items into groups according to specified criterion.
A = { 3 1 6 2 1 3 4 5 9 0 }
A = { 0 1 1 2 3 3 4 5 6 9 }
C#ODE Studio
![Page 3: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/3.jpg)
Sorting
• Sorting = ordering. • Sorted = ordered based on a particular way. • Generally, collections of data are presented in a sorted
manner. • Examples of Sorting:
– Words in a dictionary are sorted (and case distinctions are ignored).
– Files in a directory are often listed in sorted order. – The index of a book is sorted (and case
distinctions are ignored).
C#ODE Studio
![Page 4: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/4.jpg)
Sorting: Cont’d
– Many banks provide statements that list checks in increasing order (by check number).
– In a newspaper, the calendar of events in a schedule is generally sorted by date.
– Musical compact disks in a record store are generally sorted by recording artist.
• Why? – Imagine finding the phone number of your friend
in your mobile phone, but the phone book is not sorted.
C#ODE Studio
![Page 5: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/5.jpg)
Review of Complexity
Most of the primary sorting algorithms run on different space and time complexity.
Time Complexity is defined to be the time the computer takes to run a program (or algorithm
in our case).
Space complexity is defined to be the amount of memory the computer needs to run a program.
C#ODE Studio
![Page 6: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/6.jpg)
Complexity (cont.)
Complexity in general, measures the algorithms efficiency in internal factors such
as the time needed to run an algorithm.
External Factors (not related to complexity):
Size of the input of the algorithm
Speed of the Computer
Quality of the Compiler
C#ODE Studio
![Page 7: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/7.jpg)
●An algorithm or function T(n) is O(f(n)) whenever T(n)'s rate of growth is less than or equal to f(n)'s rate.
●An algorithm or function T(n) is Ω(f(n)) whenever T(n)'s rate of growth is greater than or equal to f(n)'s rate.
●An algorithm or function T(n) is Θ(f(n)) if and only if the rate of growth of T(n) is equal to f(n).
O(n), Ω(n), & Θ(n)
C#ODE Studio
![Page 8: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/8.jpg)
Types of Sorting Algorithms
There are many, many different types of sorting algorithms, but the primary ones are:
● Bubble Sort ● Selection Sort ● Insertion Sort ● Merge Sort ●Quick Sort ● Shell Sort
●Radix Sort ● Swap Sort ●Heap Sort
C#ODE Studio
![Page 9: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/9.jpg)
Bubble Sort: Idea
• Idea: bubble in water.
– Bubble in water moves upward. Why?
• How?
– When a bubble moves upward, the water from above will move downward to fill in the space left by the bubble.
C#ODE Studio
![Page 10: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/10.jpg)
Bubble Sort Example
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7 6, 2, 9, 11, 9, 3, 12, 7 6, 2, 9, 11, 9, 3, 7, 12 The 12 is greater than the 7 so they are exchanged.
The 12 is greater than the 3 so they are exchanged.
The twelve is greater than the 9 so they are exchanged
The 12 is larger than the 11 so they are exchanged.
In the third comparison, the 9 is not larger than the 12 so no
exchange is made. We move on to compare the next pair without
any change to the list.
Now the next pair of numbers are compared. Again the 9 is the
larger and so this pair is also exchanged.
Bubblesort compares the numbers in pairs from left to right
exchanging when necessary. Here the first number is compared
to the second and as it is larger they are exchanged.
The end of the list has been reached so this is the end of the first pass. The
twelve at the end of the list must be largest number in the list and so is now in
the correct position. We now start a new pass from left to right.
C#ODE Studio
![Page 11: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/11.jpg)
Bubble Sort Example
6, 2, 9, 11, 9, 3, 7, 12 2, 6, 9, 11, 9, 3, 7, 12 2, 6, 9, 9, 11, 3, 7, 12 2, 6, 9, 9, 3, 11, 7, 12 2, 6, 9, 9, 3, 7, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
Notice that this time we do not have to compare the last two
numbers as we know the 12 is in position. This pass therefore only
requires 6 comparisons.
First Pass
Second Pass
C#ODE Studio
![Page 12: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/12.jpg)
Bubble Sort Example
2, 6, 9, 9, 3, 7, 11, 12 2, 6, 9, 3, 9, 7, 11, 12 2, 6, 9, 3, 7, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12 Second Pass
First Pass
Third Pass
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
C#ODE Studio
![Page 13: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/13.jpg)
Bubble Sort Example
2, 6, 9, 3, 7, 9, 11, 12 2, 6, 3, 9, 7, 9, 11, 12 2, 6, 3, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12 Second Pass
First Pass
Third Pass
Each pass requires fewer comparisons. This time only 4 are needed.
2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
C#ODE Studio
![Page 14: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/14.jpg)
Bubble Sort Example
2, 6, 3, 7, 9, 9, 11, 12 2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12 Second Pass
First Pass
Third Pass
The list is now sorted but the algorithm does not know this until it
completes a pass with no exchanges.
2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass
C#ODE Studio
![Page 15: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/15.jpg)
Bubble Sort Example
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12 Second Pass
First Pass
Third Pass
2, 6, 9, 3, 7, 9, 11, 12 Fourth Pass
2, 6, 3, 7, 9, 9, 11, 12 Fifth Pass
Sixth Pass 2, 3, 6, 7, 9, 9, 11, 12
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With
other lists this check could save much more work.
C#ODE Studio
![Page 16: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/16.jpg)
Bubble Sort Example Quiz Time
1. Which number is definitely in its correct position at the
end of the first pass?
Answer: The last number must be the largest.
Answer: Each pass requires one fewer comparison than the last.
Answer: When a pass with no exchanges occurs.
2. How does the number of comparisons required change as
the pass number increases?
3. How does the algorithm know when the list is sorted?
4. What is the maximum number of comparisons required
for a list of 10 numbers?
Answer: 9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45
C#ODE Studio
![Page 17: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/17.jpg)
Bubble Sort: Example
• Notice that at least one element will be in the correct position each iteration.
40 2 1 43 3 65 0 -1 58 3 42 4
65 2 1 40 3 43 0 -1 58 3 42 4
65 58 1 2 3 40 0 -1 43 3 42 4
1 2 3 40 0 65 -1 43 58 3 42 4
1
2
3
4
C#ODE Studio
![Page 18: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/18.jpg)
1 0 -1 3 2 65 3 43 58 42 40 4
Bubble Sort: Example
0 -1 1 2 65 3 43 58 42 40 4 3
-1 0 1 2 65 3 43 58 42 40 4 3
6
7
8
1 2 0 3 -1 3 40 65 43 58 42 4 5
C#ODE Studio
![Page 19: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/19.jpg)
• #include<stdio.h>
• #include<conio.h>
• #include<stdio.h>
• void bubble_sort(int [],int);
• int main()
• {
• int a[30],n,i;
• printf("\nEnter no of elements :");
• scanf("%d",&n);
• printf("\nEnter array elements :");
• for(i=0;i<n;i++)
• scanf("%d",&a[i]);
• bubble_sort(a,n);
• getch();
• }
• //--------------------------------- Function
C#ODE Studio
![Page 20: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/20.jpg)
• void bubble_sort(int a[],int n)
• {
• int i,j,k,temp;
• printf("\nUnsorted Data:\n");
• for(k=0;k<n;k++)
• printf("%5d",a[k]);
• for(i=1;i< n;i++)
• {
• for(j=0;j< n-1;j++)
• if(a[j]>a[j+1])
• {
• temp=a[j];
• a[j]=a[j+1];
• a[j+1]=temp;
• printf("\nafter interchange\n");
• for(k=0;k< n;k++)
• printf("%5d",a[k]);
• getch();
• }
• printf("\n\nAfter pass %d :\n ",i);
• for(k=0;k< n;k++)
• printf("%5d",a[k]);
• }
• }
C#ODE Studio
![Page 21: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/21.jpg)
Bubble Sort: Analysis
• Running time:
– Worst case: O(N2)
– Best case: O(N)
• Variant:
– bi-directional bubble sort
• original bubble sort: only works to one direction
• bi-directional bubble sort: works back and forth.
C#ODE Studio
![Page 22: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/22.jpg)
• #include<stdio.h>
• #include<string.h>
• #include<conio.h>
• int main()
• {
• char s[5][20],t[20];
• int i,j;
• //clrscr();
• printf("Enter any five strings : \n");
• for(i=0;i<5;i++)
• scanf("%s",s[i]);
• for(i=1;i<5;i++)
• {
• for(j=1;j<5;j++)
• {
• if(strcmp(s[j-1],s[j])>0)
• {
• strcpy(t,s[j-1]);
• strcpy(s[j-1],s[j]);
• strcpy(s[j],t);
• }
• }
• }
• printf("Strings in order are : ");
• for(i=0;i<5;i++)
• printf("\n%s",s[i]);
• getch();
• }
C#ODE Studio
![Page 23: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/23.jpg)
Selection Sort: Idea
1. We have two group of items: – sorted group, and
– unsorted group
2. Initially, all items are in the unsorted group. The sorted group is empty. – We assume that items in the unsorted group
unsorted.
– We have to keep items in the sorted group sorted.
C#ODE Studio
![Page 24: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/24.jpg)
Selection Sort: Cont’d
1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group.
2. Repeat the process until the unsorted group becomes empty.
C#ODE Studio
![Page 25: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/25.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 26: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/26.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 27: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/27.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 28: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/28.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 29: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/29.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 30: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/30.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 31: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/31.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 32: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/32.jpg)
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Largest
C#ODE Studio
![Page 33: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/33.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 34: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/34.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 35: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/35.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 36: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/36.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 37: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/37.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 38: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/38.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 39: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/39.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 40: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/40.jpg)
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Largest
C#ODE Studio
![Page 41: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/41.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 42: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/42.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 43: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/43.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 44: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/44.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 45: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/45.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 46: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/46.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 47: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/47.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
C#ODE Studio
![Page 48: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/48.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 49: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/49.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 50: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/50.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 51: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/51.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 52: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/52.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 53: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/53.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
C#ODE Studio
![Page 54: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/54.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 55: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/55.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 56: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/56.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 57: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/57.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 58: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/58.jpg)
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
C#ODE Studio
![Page 59: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/59.jpg)
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
C#ODE Studio
![Page 60: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/60.jpg)
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
DONE!
C#ODE Studio
![Page 61: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/61.jpg)
42 40 2 1 3 3 4 0 -1 65 58 43
40 2 1 43 3 4 0 -1 42 65 58 3
40 2 1 43 3 4 0 -1 58 3 65 42
40 2 1 43 3 65 0 -1 58 3 42 4
Selection Sort: Example
C#ODE Studio
![Page 62: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/62.jpg)
42 40 2 1 3 3 4 0 65 58 43 -1
42 -1 2 1 3 3 4 0 65 58 43 40
42 -1 2 1 3 3 4 65 58 43 40 0
42 -1 2 1 0 3 4 65 58 43 40 3
Selection Sort: Example
C#ODE Studio
![Page 63: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/63.jpg)
1
42 -1 2 1 3 4 65 58 43 40 3 0
42 -1 0 3 4 65 58 43 40 3 2
1 42 -1 0 3 4 65 58 43 40 3 2
1 42 0 3 4 65 58 43 40 3 2 -1
1 42 0 3 4 65 58 43 40 3 2 -1
Selection Sort: Example
C#ODE Studio
![Page 64: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/64.jpg)
• #include<stdio.h> • #include<conio.h> • main() • { • int a[100], i, j, t, min, n; • printf("\nSelection Sort Program in C\n"); • printf("\nEnter the number of values\n"); • scanf("%d",&n); • printf("\nEnter the values:\n"); • for(i=1;i<=n;i++) • scanf("%d",&a[i]); • for(i=1;i<=n;i++) • { • min=i; • for(j=i+1;j<=n;j++) • { • if (a[min]>a[j]) • min=j; • } • if(min!=i) • { • t=a[i]; • a[i]=a[min]; • a[min]=t; • } • } • printf("\nSorted list in ascending order:\n"); • for(i=1;i<=n;i++) • printf("%d\n",a[i]); • getch(); • }
C#ODE Studio
![Page 65: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/65.jpg)
Selection Sort: Analysis
• Running time:
– Worst case: O(N2)
– Best case: O(N2)
C#ODE Studio
![Page 66: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/66.jpg)
Insertion Sort: Idea
• Idea: sorting cards.
– 8 | 5 9 2 6 3
– 5 8 | 9 2 6 3
– 5 8 9 | 2 6 3
– 2 5 8 9 | 6 3
– 2 5 6 8 9 | 3
– 2 3 5 6 8 9 |
C#ODE Studio
![Page 67: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/67.jpg)
Insertion Sort: Idea 1. We have two group of items:
– sorted group, and – unsorted group
2. Initially, all items in the unsorted group and the sorted group is empty. – We assume that items in the unsorted group unsorted. – We have to keep items in the sorted group sorted.
3. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property.
4. Repeat the process until the unsorted group becomes empty.
C#ODE Studio
![Page 68: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/68.jpg)
40 2 1 43 3 65 0 -1 58 3 42 4
2 40 1 43 3 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
40
Insertion Sort: Example
C#ODE Studio
![Page 69: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/69.jpg)
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 40 43 3 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
Insertion Sort: Example
C#ODE Studio
![Page 70: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/70.jpg)
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 -1 58 3 42 4
1 2 3 40 43 65 0 58 3 42 4 1 2 3 40 43 65 0 -1
Insertion Sort: Example
C#ODE Studio
![Page 71: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/71.jpg)
1 2 3 40 43 65 0 58 3 42 4 1 2 3 40 43 65 0 -1
1 2 3 40 43 65 0 58 42 4 1 2 3 3 43 65 0 -1 58 40 43 65
1 2 3 40 43 65 0 42 4 1 2 3 3 43 65 0 -1 58 40 43 65
Insertion Sort: Example
1 2 3 40 43 65 0 42 1 2 3 3 43 65 0 -1 58 4 43 65 42 58 40 43 65
C#ODE Studio
![Page 72: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/72.jpg)
• #include<stdio.h>
• #include<conio.h>
• int main(){
• int i,j,num,temp,a[20];
• printf("Enter total elements: ");
• scanf("%d",&num);
• printf("Enter %d elements: ",num);
• for(i=0;i<num;i++)
• scanf("%d",&a[i]);
• for(i=1;i<num;i++){
• temp=a[i];
• j=i-1;
• while((temp<a[j])&&(j>=0))
• {
• a[j+1]=a[j];
• j=j-1;
• }
• a[j+1]=temp;
• }
• printf("After Sorting: ");
• for(i=0;i<num;i++)
• printf("%d",a[i]);
• getch();
• return 0;
• }
C#ODE Studio
![Page 73: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/73.jpg)
Insertion Sort: Analysis
• Running time analysis:
– Worst case: O(N2)
– Best case: O(N)
C#ODE Studio
![Page 74: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/74.jpg)
A Lower Bound
• Bubble Sort, Selection Sort, Insertion Sort all have worst case of O(N2).
• Turns out, for any algorithm that exchanges adjacent items, this is the best worst case: Ω(N2)
• In other words, this is a lower bound!
C#ODE Studio
![Page 75: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/75.jpg)
Mergesort
•Mergesort (divide-and-conquer)
–Divide array into two halves.
A L G O R I T H M S
divide A L G O R I T H M S
C#ODE Studio
![Page 76: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/76.jpg)
Mergesort
•Mergesort (divide-and-conquer)
–Divide array into two halves.
–Recursively sort each half.
sort
A L G O R I T H M S
divide A L G O R I T H M S
A G L O R H I M S T
C#ODE Studio
![Page 77: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/77.jpg)
Mergesort
•Mergesort (divide-and-conquer)
–Divide array into two halves.
–Recursively sort each half.
–Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divide A L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
C#ODE Studio
![Page 78: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/78.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
A
C#ODE Studio
![Page 79: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/79.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
G
C#ODE Studio
![Page 80: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/80.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
H
C#ODE Studio
![Page 81: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/81.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
I
C#ODE Studio
![Page 82: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/82.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
L
C#ODE Studio
![Page 83: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/83.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
M
C#ODE Studio
![Page 84: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/84.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
O
C#ODE Studio
![Page 85: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/85.jpg)
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
R
C#ODE Studio
![Page 86: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/86.jpg)
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
S
C#ODE Studio
![Page 87: Sorting Technique - 1](https://reader030.vdocument.in/reader030/viewer/2022032711/577ccd251a28ab9e788ba018/html5/thumbnails/87.jpg)
auxiliary array
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S
Merging
•Merge. – Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
T
C#ODE Studio