Upload
noah-merritt
View
214
Download
1
Embed Size (px)
Citation preview
Sorting
Leena A
PGT Comp SC
KV No:2 Jalahalli
Introduction
• Common problem: sort a list of values, starting from lowest to highest. – List of exam scores
– Words of dictionary in alphabetical order
– Students names listed alphabetically
– Student records sorted by ID#
Quadratic Sorting Algorithms
• We are given n records to sort.
Three Sorting techniques are used normally
– Selection sort– Insertion sort– Bubble sort
Sorting an Array of IntegersSorting an Array of Integers
• Example: we are given an array of six integers that we want to sort from smallest to largest
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6][0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Start by finding the smallest entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Swap the smallest entry with the first entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Swap the smallest entry with the first entry.
[0] [1] [2] [3] [4] [5]
0
10
20
30
40
50
60
70
[1] [2] [3] [4] [5] [6]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Part of the array is now sorted.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Find the smallest element in the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• Swap with the front of the unsorted side.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• We have increased the size of the sorted side by one element.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• The process continues...
Sorted side Unsorted side
Smallestfrom
unsorted
Smallestfrom
unsorted
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• The process continues...
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
Swap
with
front
Swap
with
front
The Selection Sort AlgorithmThe Selection Sort Algorithm
• The process continues...
Sorted side Unsorted sideSorted side
is bigger
Sorted sideis bigger
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• The process keeps adding one more number to the sorted side.
• The sorted side has the smallest numbers, arranged from small to large.
Sorted side Unsorted side
[0] [1] [2] [3] [4] [5]
The Selection Sort AlgorithmThe Selection Sort Algorithm
• We can stop when the unsorted side has just one number, since that number must be the largest number.
[0] [1] [2] [3] [4] [5]
Sorted side Unsorted side
The Selection Sort AlgorithmThe Selection Sort Algorithm
• The array is now sorted.
• We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. [0] [1] [2] [3] [4] [5]
void selectionsort(int a[], int size){int small,pos,t;for(int i=0;i<size;i++){small=a[i];for(int j= i+1;j<size;j++){
if(a[j]<small){small=a[j];pos=j;}
}t=a[i];a[i]=a[pos];a[pos]=t;}}
1. Write a function to sort the student records based on their marks in ascending order.
The records are implemented as followsstruct student
{int rno;char name[20];float marks;
};
Review Questions
Thank You