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

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

Embed Size (px)

Citation preview

Page 1: 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

Sorting

Leena A

PGT Comp SC

KV No:2 Jalahalli

Page 2: 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

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#

Page 3: 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

Quadratic Sorting Algorithms

• We are given n records to sort.

Three Sorting techniques are used normally

– Selection sort– Insertion sort– Bubble sort

Page 4: 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

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]

Page 5: 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

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]

Page 6: 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

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]

Page 7: 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

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]

Page 8: 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

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]

Page 9: 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

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]

Page 10: 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

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]

Page 11: 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

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]

Page 12: 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

The Selection Sort AlgorithmThe Selection Sort Algorithm

• The process continues...

Sorted side Unsorted side

Smallestfrom

unsorted

Smallestfrom

unsorted

[0] [1] [2] [3] [4] [5]

Page 13: 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

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

Page 14: 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

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]

Page 15: 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

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]

Page 16: 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

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

Page 17: 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

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]

Page 18: 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

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

Page 19: 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

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

Page 20: 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

Thank You