Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Sorting Leena A PGT Comp SC KV No:2 Jalahalli

2 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#

3 Quadratic Sorting Algorithms We are given n records to sort. Three Sorting techniques are used normally –Selection sort –Insertion sort –Bubble sort

4 Sorting an Array of Integers Example: we are given an array of six integers that we want to sort from smallest to largest [0] [1] [2] [3] [4] [5]

5 The Selection Sort Algorithm Start by finding the smallest entry. [0] [1] [2] [3] [4] [5]

6 The Selection Sort Algorithm Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]

7 The Selection Sort Algorithm Swap the smallest entry with the first entry. [0] [1] [2] [3] [4] [5]

8 The Selection Sort Algorithm Part of the array is now sorted. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

9 The Selection Sort Algorithm Find the smallest element in the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

10 The Selection Sort Algorithm Swap with the front of the unsorted side. Sorted side Unsorted side [0] [1] [2] [3] [4] [5]

11 The 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]

12 The Selection Sort Algorithm The process continues... Sorted side Unsorted side Smallest from unsorted Smallest from unsorted [0] [1] [2] [3] [4] [5]

13 The Selection Sort Algorithm The process continues... Sorted side Unsorted side [0] [1] [2] [3] [4] [5] Swap with front Swap with front

14 The Selection Sort Algorithm The process continues... Sorted side Unsorted side Sorted side is bigger Sorted side is bigger [0] [1] [2] [3] [4] [5]

15 The 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]

16 The 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

17 The 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]

18 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; }}

19 1.Write a function to sort the student records based on their marks in ascending order. The records are implemented as follows struct student { int rno; char name[20]; float marks; }; Review Questions

20 Thank You


Download ppt "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."

Similar presentations


Ads by Google