Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

191
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University

Transcript of Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Page 1: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Sorting Techniques

Rizwan RehmanCentre for Computer StudiesDibrugarh University

Page 2: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Bubble Sort

Page 3: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Sorting

• Sorting takes an unordered collection and makes it an ordered one.

512354277 101

1 2 3 4 5 6

5 12 35 42 77 101

1 2 3 4 5 6

Page 4: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 5: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 6: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 7: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 8: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 9: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 10: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

"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 11: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

The “Bubble Up” Algorithm

index <- 1last_compare_at <- n – 1

loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1endloop

Page 12: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

No, Swap isn’t built in.

Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- tendprocedure // Swap

LB

Page 13: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Items of Interest

• Notice that only the largest value is correctly placed

• All other values are still out of order• So we need to repeat this process

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 14: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Repeat “Bubble Up” How Many Times?

• If we have N elements…

• And if each time we bubble an element, we place it in its correct location…

• Then we repeat the “bubble up” process N – 1 times.

• This guarantees we’ll correctly place all N elements.

Page 15: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

“Bubbling” All the Elements

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

42 35 12 5 771 2 3 4 5 6

101

N -

1

Page 16: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Reducing the Number of Comparisons

12 35 42 77 1011 2 3 4 5 6

5

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

Page 17: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Reducing the Number of Comparisons

• On the Nth “bubble up”, we only need to do MAX-N comparisons.

• For example:– This is the 4th “bubble up”– MAX is 6– Thus we have 2 comparisons to do

42 5 3512 771 2 3 4 5 6

101

Page 18: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Putting It All Together

Page 19: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

N is … // Size of Array

Arr_Type definesa Array[1..N] of Num

Procedure Swap(n1, n2 isoftype in/out Num) temp isoftype Num temp <- n1 n1 <- n2 n2 <- tempendprocedure // Swap

Page 20: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1

loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloopendprocedure // Bubblesort

Inn

er l

oo

p

Ou

ter

loo

p

Page 21: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Already Sorted Collections?

• What if the collection was already sorted?• What if only a few elements were out of place and

after a couple of “bubble ups,” the collection was sorted?

• We want to be able to detect this and “stop early”!

42 35 12 5 771 2 3 4 5 6

101

Page 22: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Using a Boolean “Flag”

• We can use a boolean variable to determine if any swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the collection is already sorted!

• This boolean “flag” needs to be reset after each “bubble up.”

Page 23: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

did_swap isoftype Boolean did_swap <- true

loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop

Page 24: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 25: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 26: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 27: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 28: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 29: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 30: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 31: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 32: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 33: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 34: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 35: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 36: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 37: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 38: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 39: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 40: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 41: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 42: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 43: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 44: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 45: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 46: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 47: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 48: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 49: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 50: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 51: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 52: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 53: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 54: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 55: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 56: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 57: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 58: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 59: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 60: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 61: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 62: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 63: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 64: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 65: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 66: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 67: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 68: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 69: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 70: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 71: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 72: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 73: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 74: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 75: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 76: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 77: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 78: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 79: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 80: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 81: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 82: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 83: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 84: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 85: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 86: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 87: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 88: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 89: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 90: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 91: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 92: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 93: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 94: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 95: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 96: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

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 97: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Summary

• “Bubble Up” algorithm will move largest value to its correct location (to the right)

• Repeat “Bubble Up” until all elements are correctly placed:– Maximum of N-1 times– Can finish early if no swapping occurs

• We reduce the number of elements we compare each time one is correctly placed

Page 98: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Truth in CS Act

• NOBODY EVER USES BUBBLE SORT

• NOBODY

• NOT EVER

• BECAUSE IT IS EXTREMELY INEFFICIENT

LB

Page 99: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Mergesort

Page 100: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Sorting

• Sorting takes an unordered collection and makes it an ordered one.

512354277 101

1 2 3 4 5 6

5 12 35 42 77 101

1 2 3 4 5 6

Page 101: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Divide and Conquer

• Divide and Conquer cuts the problem in half each time, but uses the result of both halves:– cut the problem in half until the problem

is trivial – solve for both halves– combine the solutions

Page 102: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Mergesort• A divide-and-conquer algorithm:• Divide the unsorted array into 2 halves until the

sub-arrays only contain one element• Merge the sub-problem solutions together:

– Compare the sub-array’s first elements– Remove the smallest element and put it into

the result array– Continue the process until all elements have

been put into the result array

37 23 6 89 15 12 2 19

Page 103: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Algorithm

Mergesort(Passed an array) if array size > 1

Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves.

Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)

Page 104: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

More TRUTH in CS

• We don’t really pass in two arrays!

• We pass in one array with indicator variables which tell us where one set of data starts and finishes and where the other set of data starts and finishes.

• Honest.

s1 f1 s2 f2

LB

Page 105: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Algorithm

Mergesort(Passed an array) if array size > 1

Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves.

Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)

LB

Page 106: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

Page 107: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

Page 108: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

Page 109: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398

Page 110: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398

Merge

Page 111: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398

23

Merge

Page 112: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398

23 98

Merge

Page 113: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

23 98

Page 114: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

23 98

Page 115: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

14

Merge

23 98

Page 116: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

45

Merge

23 98 14

Page 117: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

98 451423

Page 118: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

98 14

14

23 45

Page 119: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

23 14

14 23

98 45

Page 120: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

23 98 4514

14 23 45

Page 121: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

Merge

23 98 4514

14 23 45 98

Page 122: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

23 98 4514

14 23 45 98

Page 123: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676

23 98 4514

14 23 45 98

Page 124: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676

Merge

23 98 4514

14 23 45 98

Page 125: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676

6

Merge

23 98 4514

14 23 45 98

Page 126: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676

67

Merge

23 98 4514 6

14 23 45 98

Page 127: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

23 98 4514 676

14 23 45 98

Page 128: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676

14 23 45 98

Page 129: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

3323 98 4514 676

14 23 45 98

Page 130: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

4223 98 4514 676 33

14 23 45 98

Page 131: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 45 98

Page 132: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 6 4233

14 23 45 98 6

67

Page 133: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 6 33

14 23 45 98 6 33

67 42

Page 134: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 6 4233

14 23 45 98 6 33 42

67

Page 135: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 45 98 6 33 42 67

Page 136: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

23 45 98 33 42 6714 6

Page 137: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

23 45 98 6 42 67

6

14 33

Page 138: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 45 98 6 42 67

6 14

23 33

Page 139: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 98 6 42 67

6 14 23

45 33

Page 140: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 98 6 33 67

6 14 23 33

45 42

Page 141: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 98 6 33 42

6 14 23 33 42

45 67

Page 142: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 45 6 33 42

6 14 23 33 42 45

98 67

Page 143: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Page 144: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

Merge

23 98 4514 676 4233

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Page 145: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

674523 14 6 3398 42

4523 1498

2398 45 14

676 33 42

676 33 42

23 98 4514 676 4233

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Page 146: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

674523 14 6 3398 42

6 14 23 33 42 45 67 98

Page 147: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Summary

• Divide the unsorted collection into two

• Until the sub-arrays only contain one

element

• Then merge the sub-problem solutions together

Page 148: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 149: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Page 150: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Page 151: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Page 152: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Page 153: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Page 154: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Page 155: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 6 2

Largest

Page 156: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 157: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 158: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 159: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 160: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 161: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 162: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Page 163: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

5 1 3 4 2 6

Largest

Page 164: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 165: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 166: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 167: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 168: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 169: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 170: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Largest

Page 171: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 172: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 173: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 174: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 175: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 176: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Largest

Page 177: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 178: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 179: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 180: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Page 181: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

2 1 3 4 5 6

Largest

Page 182: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

1 2 3 4 5 6

Page 183: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Selection Sort

1 2 3 4 5 6

DONE!

Page 184: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Insertion Sort

23

17

45

18

12

221 2 6543

Page 185: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

23

17

45

18

12

221 2 6543

1 2 6543

Page 186: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

23

17

45

18

12

221 2 6543

1 2 6543

Page 187: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

23

17

45

18

12

221 2 6543

1 2 6543

Page 188: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

23

17

45

18

12

221 2 6543

1 2 6543

Page 189: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

23

17

45

18

12

221 2 6543

1 2 6543

Page 190: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

Example: sorting numbered cards

18

12

22

17

23

45

1 2 6543

1 2 6543

Page 191: Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.

THANK YOU