Sorting techniques Anil Dutt

143
C-Programming ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sortin g

Transcript of Sorting techniques Anil Dutt

Page 1: Sorting techniques Anil Dutt

C-Programming----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sorting

Page 2: Sorting techniques Anil Dutt

TOPICS TO BE COVERED

Introduction to Sorting Bubble Sort

Selection Sort Insertion Sort

Page 3: Sorting techniques Anil Dutt

Sorting

Page 4: Sorting techniques Anil Dutt

Sorting…… A fundamental application for computers Done to make finding data (searching)

faster Many different algorithms for sorting One of the difficulties with sorting is

working with a fixed size storage container (array) if resize, that is expensive (slow)

The "simple" sorts run in quadratic time O(N2) bubble sort selection sort insertion sort

4

Page 5: Sorting techniques Anil Dutt

Sorting…… To arrange a set of items in sequence. It is estimated that 25~50% of all

computing power is used for sorting activities.

Possible reasons: Many applications require sorting; Many applications perform sorting when

they don't have to; Many applications use inefficient sorting

algorithms.

Page 6: Sorting techniques Anil Dutt

Sorting Applications To prepare a list of student ID, names, and

scores in a table (sorted by ID or name) for easy checking.

To prepare a list of scores before letter grade assignment.

To produce a list of horses after a race (sorted by the finishing times) for payoff calculation.

To prepare an originally unsorted array for ordered binary searching.

Page 8: Sorting techniques Anil Dutt

Bubble Sort…… Bubble sort examines the array from start

to finish, comparing elements as it goes. Any time it finds a larger element before a smaller

element, it swaps the two. In this way, the larger elements are passed towards

the end. The largest element of the array therefore

"bubbles" to the end of the array. Then it repeats the process for the unsorted portion

of the array until the whole array is sorted.

Page 9: Sorting techniques Anil Dutt

Bubble Sort…… Bubble sort works on the same general

principle as shaking a soft drink bottle. Right after shaking, the contents are a mixture

of bubbles and soft drink, distributed randomly. Because bubbles are lighter than the soft

drink, they rise to the surface, displacing the soft drink downwards.

This is how bubble sort got its name, because the smaller elements "float" to the top, while

the larger elements "sink" to the bottom.

Page 10: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

512354277 101

1 2 3 4 5 6

Page 11: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

512354277 101

1 2 3 4 5 6

Swap42 77

Page 12: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

512357742 101

1 2 3 4 5 6

Swap35 77

Page 13: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

512773542 101

1 2 3 4 5 6

Swap12 77

Page 14: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

577123542 101

1 2 3 4 5 6

No need to swap

Page 15: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

577123542 101

1 2 3 4 5 6

Swap5 101

Page 16: Sorting techniques Anil Dutt

"Bubbling Up" the Largest Element

Traverse a collection of elements Move from the front to the end “Bubble” the largest value to the end using

pair-wise comparisons and swapping

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 17: Sorting techniques Anil Dutt

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

N 8 did_swap true

Page 18: Sorting techniques Anil Dutt

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8 did_swap false

Page 19: Sorting techniques Anil Dutt

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8

Swap

did_swap false

Page 20: Sorting techniques Anil Dutt

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8

Swap

did_swap true

Page 21: Sorting techniques Anil Dutt

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8 did_swap true

Page 22: Sorting techniques Anil Dutt

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8

Swap

did_swap true

Page 23: Sorting techniques Anil Dutt

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8

Swap

did_swap true

Page 24: Sorting techniques Anil Dutt

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8 did_swap true

Page 25: Sorting techniques Anil Dutt

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8

Swap

did_swap true

Page 26: Sorting techniques Anil Dutt

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8

Swap

did_swap true

Page 27: Sorting techniques Anil Dutt

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8 did_swap true

Page 28: Sorting techniques Anil Dutt

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8

Swap

did_swap true

Page 29: Sorting techniques Anil Dutt

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8

Swap

did_swap true

Page 30: Sorting techniques Anil Dutt

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8 did_swap true

Page 31: Sorting techniques Anil Dutt

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8

Swap

did_swap true

Page 32: Sorting techniques Anil Dutt

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8

Swap

did_swap true

Page 33: Sorting techniques Anil Dutt

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8 did_swap true

Page 34: Sorting techniques Anil Dutt

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8

Swap

did_swap true

Page 35: Sorting techniques Anil Dutt

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8

Swap

did_swap true

Page 36: Sorting techniques Anil Dutt

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8 did_swap true

Page 37: Sorting techniques Anil Dutt

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8

Swap

did_swap true

Page 38: Sorting techniques Anil Dutt

An Animated Example

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8

Swap

did_swap true

Page 39: Sorting techniques Anil Dutt

After First Pass of Outer Loop

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

7

8

N 8

Finished first “Bubble Up”

did_swap true

Page 40: Sorting techniques Anil Dutt

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

1

N 8 did_swap false

Page 41: Sorting techniques Anil Dutt

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

1

N 8 did_swap false

No Swap

Page 42: Sorting techniques Anil Dutt

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap false

Page 43: Sorting techniques Anil Dutt

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap false

Swap

Page 44: Sorting techniques Anil Dutt

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap true

Swap

Page 45: Sorting techniques Anil Dutt

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Page 46: Sorting techniques Anil Dutt

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Swap

Page 47: Sorting techniques Anil Dutt

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Swap

Page 48: Sorting techniques Anil Dutt

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

4

N 8 did_swap true

Page 49: Sorting techniques Anil Dutt

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

4

N 8 did_swap true

No Swap

Page 50: Sorting techniques Anil Dutt

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Page 51: Sorting techniques Anil Dutt

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Swap

Page 52: Sorting techniques Anil Dutt

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Swap

Page 53: Sorting techniques Anil Dutt

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Page 54: Sorting techniques Anil Dutt

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Swap

Page 55: Sorting techniques Anil Dutt

The Second “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Swap

Page 56: Sorting techniques Anil Dutt

After Second Pass of Outer Loop

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

6

7

N 8 did_swap true

Finished second “Bubble Up”

Page 57: Sorting techniques Anil Dutt

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap false

Page 58: Sorting techniques Anil Dutt

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap false

Swap

Page 59: Sorting techniques Anil Dutt

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap true

Swap

Page 60: Sorting techniques Anil Dutt

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Page 61: Sorting techniques Anil Dutt

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Swap

Page 62: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Swap

Page 63: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

3

N 8 did_swap true

Page 64: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

3

N 8 did_swap true

No Swap

Page 65: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Page 66: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Swap

Page 67: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Swap

Page 68: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Page 69: Sorting techniques Anil Dutt

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Swap

Page 70: Sorting techniques Anil Dutt

The Third “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Swap

Page 71: Sorting techniques Anil Dutt

After Third Pass of Outer Loop

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

6

N 8 did_swap true

Finished third “Bubble Up”

Page 72: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap false

Page 73: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap false

Swap

Page 74: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap true

Swap

Page 75: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

2

N 8 did_swap true

Page 76: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

2

N 8 did_swap true

No Swap

Page 77: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

3

N 8 did_swap true

Page 78: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

3

N 8 did_swap true

No Swap

Page 79: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

4

N 8 did_swap true

Page 80: Sorting techniques Anil Dutt

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

4

N 8 did_swap true

No Swap

Page 81: Sorting techniques Anil Dutt

After Fourth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

5

N 8 did_swap true

Finished fourth “Bubble Up”

Page 82: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

1

N 8 did_swap false

Page 83: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

1

N 8 did_swap false

No Swap

Page 84: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

2

N 8 did_swap false

Page 85: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

2

N 8 did_swap false

No Swap

Page 86: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

3

N 8 did_swap false

Page 87: Sorting techniques Anil Dutt

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

3

N 8 did_swap false

No Swap

Page 88: Sorting techniques Anil Dutt

After Fifth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

4

N 8 did_swap false

Finished fifth “Bubble Up”

Page 89: Sorting techniques Anil Dutt

Finished “Early”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

4

N 8 did_swap false

We didn’t do any swapping,so all of the other elementsmust be correctly placed.

We can “skip” the last twopasses of the outer loop.

Page 90: Sorting techniques Anil Dutt

Example: #include <stdio.h> void bubble_sort(long [], long); int main() { long array[100], n, c, d, swap; printf("Enter number of elements:"); scanf("%ld", &n); printf("Enter %ld longegers\n", n); for (c = 0; c < n; c++) scanf("%ld", &array[c]); bubble_sort(array, n); printf("Sorted list in ascending order:n"); for ( c = 0 ; c < n ; c++ ) printf("%ld\n", array[c]); return 0; }

Page 91: Sorting techniques Anil Dutt

void bubble_sort(long list[], long n) { long c, d, t; for (c = 0 ; c < ( n - 1 ); c++) {

for (d = 0 ; d < n - c - 1; d++) { if (list[d] > list[d+1]) { t = list[d]; list[d] = list[d+1]; list[d+1]= t; } } } }

bubsort1 .c

Page 92: Sorting techniques Anil Dutt

Selection Sort……

Page 93: Sorting techniques Anil Dutt

Selection Sort…… Idea:

Find the largest element in the array Exchange it with the element in the

rightmost position Find the second largest element and

exchange it with the element in the second rightmost position

Continue until the array is sorted

Page 94: Sorting techniques Anil Dutt

Before sorting 14 2 10 5 1 3 17 7

After pass 1 14 2 10 5 1 3 7 17

After pass 2 7 2 10 5 1 3 14 17

After pass 3 7 2 3 5 1 10 14 17

After pass 4 1 2 3 5 7 10 14 17

Page 95: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 96: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 97: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 98: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 99: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 100: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 101: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 102: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Largest

Page 103: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 104: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 105: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 106: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 107: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 108: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 109: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Page 110: Sorting techniques Anil Dutt

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Largest

Page 111: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 112: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 113: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 114: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 115: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 116: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 117: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 118: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 119: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 120: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 121: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 122: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 123: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 124: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 125: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 126: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 127: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Page 128: Sorting techniques Anil Dutt

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Page 129: Sorting techniques Anil Dutt

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

Page 130: Sorting techniques Anil Dutt

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

DONE!

Page 131: Sorting techniques Anil Dutt

Example:

#include <stdio.h> main() { int A[20], N, Temp, i, j; printf(" ENTER THE NUMBER OF TERMS...: "); scanf("%d",&N); printf("\n ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=1; i<=N; i++) { scanf("\n\t\t%d", &A[i]); }

Page 132: Sorting techniques Anil Dutt

for(i=1; i<=N-1; i++) for(j=i+1; j<=N;j++) if(A[i]>A[j]) { Temp = A[i]; A[i] = A[j]; A[j] = Temp; } printf("THE ASCENDING ORDER LIST IS...:\n"); for(i=1; i<=N; i++) printf("\n %d",A[i]); }

selecsort .c

Page 133: Sorting techniques Anil Dutt

Insertion Sort……

Page 134: Sorting techniques Anil Dutt

134 Insertion Sort…… Idea: like sorting a hand of playing cards

Start with an empty left hand and the cards facing down on the table.

Remove one card at a time from the table, and insert it into the correct position in the left hand

compare it with each of the cards already in the hand, from right to left

The cards held in the left hand are sorted

these cards were originally the top cards of the pile on the table

Page 135: Sorting techniques Anil Dutt

135

To insert 12, we need to make room for it by moving first 36 and then 24.

Insertion Sort

6 10 24

12

36

Page 136: Sorting techniques Anil Dutt

136

6 10 24

Insertion Sort

36

12

Page 137: Sorting techniques Anil Dutt

137

Insertion Sort

6 10 24 36

12

Page 138: Sorting techniques Anil Dutt

138 Insertion Sort

5 2 4 6 1 3

input array

left sub-array right sub-array

at each iteration, the array is divided in two sub-arrays:

sorted unsorted

Page 139: Sorting techniques Anil Dutt

139 Insertion Sort

Page 140: Sorting techniques Anil Dutt

#include<stdio.h> void main() { int A[20], N, Temp, i, j; printf("ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d",&A[i]); }

Example:

Page 141: Sorting techniques Anil Dutt

for(i=1; i<N; 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("\nTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n%d", A[i]); }

inrsot .c

Page 142: Sorting techniques Anil Dutt

THANK YOU FOR

LISTENING

Page 143: Sorting techniques Anil Dutt

THEEND