Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data...

120
Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1

Transcript of Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data...

Page 1: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Data Structures in JavaLecture 14: Sorting I

11/9/2015

Daniel Bauer

1

Page 2: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Midterm Exams

2

Page 3: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting• Input:

• Array containing unordered Comparables (duplicates allowed).

• Output:

• A sorted array containing the same items.

• Only comparisons between pairs of items allowed (comparison based sorting).

34 8 64 51 32 21

8 21 32 34 51 64

3

Page 4: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications

4

Page 5: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

4

Page 6: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

• Selection problem (find the k-th largest, find the median).

4

Page 7: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

• Selection problem (find the k-th largest, find the median).

• Efficient search (binary search on sorted data).

4

Page 8: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

• Selection problem (find the k-th largest, find the median).

• Efficient search (binary search on sorted data).

• Finding duplicates.

4

Page 9: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

• Selection problem (find the k-th largest, find the median).

• Efficient search (binary search on sorted data).

• Finding duplicates.

• Greedy algorithms (explore k highest scoring paths first).

4

Page 10: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Applications• Sorting email by date / subject …, Sorting files by name.

• Selection problem (find the k-th largest, find the median).

• Efficient search (binary search on sorted data).

• Finding duplicates.

• Greedy algorithms (explore k highest scoring paths first).

• …

4

Page 11: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Overview• We will discuss different sorting algorithms and compare their

running time, required space, and stability.

• Insertion sort

• Shell sort

• Heap sort

• Merge sort

• Quick sort

• Radix Sort

5

Page 12: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort34 8 64 51 32 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

p=1

6

Page 13: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 64 51 32 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

p=1

6

Page 14: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 64 51 32 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8

p=2

64

6

Page 15: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 32 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 64

p=3

51

6

Page 16: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 32 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 64

p=3

51

6

Page 17: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 6451 32

p=4

6

Page 18: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 6451 32

p=4

6

Page 19: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132

p=4

6

Page 20: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348 21

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132

p=4

6

Page 21: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132 21

p=5

6

Page 22: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132 21

p=5

6

Page 23: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132 21

p=5

6

Page 24: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 645132 21

p=5

6

Page 25: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort348

• Perform p=1…N-1 passes through the array. • Assume array[0..p-1] is already sorted. • Take the element x at position p.

• Repeatedly swap x its left neighbor until it is in the correct position.

8 64513221

p=5

6

Page 26: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort void  insertionSort(  Integer  [  ]  a  )  {          int  j;  

       for(  int  p  =  1;  p  <  a.length;  p++  )  {                  Integer  x  =  a[  p  ];                  for(  j  =  p;  j  >  0  &&  x  <  a[j  -­‐  1];  j-­‐-­‐  )                          a[  j  ]  =  a[  j  -­‐  1  ];                  a[  j  ]  =  x;          }  }

7

Page 27: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort void  insertionSort(  Integer  [  ]  a  )  {          int  j;  

       for(  int  p  =  1;  p  <  a.length;  p++  )  {                  Integer  x  =  a[  p  ];                  for(  j  =  p;  j  >  0  &&  x  <  a[j  -­‐  1];  j-­‐-­‐  )                          a[  j  ]  =  a[  j  -­‐  1  ];                  a[  j  ]  =  x;          }  }

O(N)

O(N)

Total: O(N2)

7

Page 28: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort void  insertionSort(  Integer  [  ]  a  )  {          int  j;  

       for(  int  p  =  1;  p  <  a.length;  p++  )  {                  Integer  x  =  a[  p  ];                  for(  j  =  p;  j  >  0  &&  x  <  a[j  -­‐  1];  j-­‐-­‐  )                          a[  j  ]  =  a[  j  -­‐  1  ];                  a[  j  ]  =  x;          }  }

O(N)

O(N)

Total: O(N2)Best case input (sorted): O(N)

7

Page 29: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Insertion Sort void  insertionSort(  Integer  [  ]  a  )  {          int  j;  

       for(  int  p  =  1;  p  <  a.length;  p++  )  {                  Integer  x  =  a[  p  ];                  for(  j  =  p;  j  >  0  &&  x  <  a[j  -­‐  1];  j-­‐-­‐  )                          a[  j  ]  =  a[  j  -­‐  1  ];                  a[  j  ]  =  x;          }  }

O(N)

O(N)

Total: O(N2)

Worst case input (sorted in reverse order):Best case input (sorted): O(N)

7

Page 30: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

34 8 64 51 32 21 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 18

Page 31: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

348 64 51 3221 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 18

Page 32: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 8 64 51 32 34 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 19

Page 33: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 864 51 32 347 30 1

h3 = 5

2 5

h2 = 3 h1 = 19

Page 34: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 64 51 32 34 8 30 1

h3 = 5

2 5

h2 = 3 h1 = 110

Page 35: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 6451 32 34 830 1

h3 = 5

2 5

h2 = 3 h1 = 110

Page 36: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 51 32 34 8 64 1

h3 = 5

2 5

h2 = 3 h1 = 111

Page 37: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 5132 34 8 641

h3 = 5

2 5

h2 = 3 h1 = 111

Page 38: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 32 34 8 64 51

h3 = 5

2 5

h2 = 3 h1 = 112

Page 39: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 3234 8 64 51

h3 = 5

2 5

h2 = 3 h1 = 112

Page 40: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 2 34 8 64 51

h3 = 5

32 5

h2 = 3 h1 = 113

Page 41: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 2 348 64 51

h3 = 5

325

h2 = 3 h1 = 113

Page 42: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

217 30 1 2 348 64 51

h3 = 5

325

h2 = 3 h1 = 113

Page 43: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

5 7 30 1 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 114

Page 44: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

57 301 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 114

Page 45: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 7 30 5 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 115

Page 46: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 730 52 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 115

Page 47: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 30 5 7 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 116

Page 48: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 305 721 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 116

Page 49: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 117

Page 50: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 118

Page 51: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 119

Page 52: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 120

Page 53: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 121

Page 54: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 6451

h3 = 5

3234

h2 = 3 h1 = 121

Page 55: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 122

Page 56: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 123

Page 57: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 124

Page 58: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 215 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 124

Page 59: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 21 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 125

Page 60: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 217 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 125

Page 61: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 126

Page 62: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 127

Page 63: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 308 34 51

h3 = 5

32 64

h2 = 3 h1 = 127

Page 64: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 308 34 51

h3 = 5

32 64

h2 = 3 h1 = 127

Page 65: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 128

Page 66: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 129

Page 67: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 130

Page 68: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 130

Page 69: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 130

Page 70: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• Generalize insertion sort so that items that are

further apart can be swapped.

• Break up sorting into phases. Each phase k makes sure that all items space hk apart are sorted.

• “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 32 34

h3 = 5

51 64

h2 = 3 h1 = 131

Page 71: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Shell Sort• The running time analysis for shell sort is complex

and depends on the specific increment sequence.

• With Hibbard’s sequence (1,3,7,15,…,2k-1) worst case running time is

32

Page 72: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Stability• Assume we put key/value pairs sorted by keys into the

array.

• Shell Sort is unstable: keys will be sorted, but values for the same key may be in different order than in the input.

34 8 64 1 4 3 30 7 2 5

val1

1

val233

Page 73: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Sorting Stability• Assume we put key/value pairs sorted by keys into the

array.

• Shell Sort is unstable: keys will be sorted, but values for the same key may be in different order than in the input.

348 64 1 4 3 30 7 2 5

val1

1

val2 33

Page 74: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Space Requirements• Both Insertion Sort and Shell Sort operate in place.

• Only a small amount of memory required to store a temporary value for swaps.

• Space requirement: O(1)

34

Page 75: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort• First convert an unordered array into a heap in

O(N) time.

• Then perform N deleteMin operations to retrieve the elements in sorted order.

• each deleteMin is O(log N)

35

Page 76: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort• First convert an unordered array into a heap in

O(N) time.

• Then perform N deleteMin operations to retrieve the elements in sorted order.

• each deleteMin is O(log N)

• Problem: This algorithm requires a second array to store the output: O(N) space!

• Idea: re-use the freed space after each deleteMin.35

Page 77: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

5 4 6 9 1 8 3 10 7 2 1136

Page 78: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

1

2

3

4

5

6

7

89

10 11

5 4 6 9 1 8 3 10 7 2 1136

Page 79: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

2

1

4

5 11

1 2 3 7 4 8 6 10 9 5 11

Build heap in O(N) time

37

Page 80: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

2

1

4

5 11

1 2 3 7 4 8 6 10 9 5 11

Build heap in O(N) time

37

Page 81: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

2

1

4

5 11

2 3 7 4 8 6 10 9 51 11

deleteMin, write min element into empty cell

38

Page 82: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

2

4

5

11

2 3 7 4 8 6 10 9 5 111

deleteMin, write min element into empty cell

38

Page 83: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

4

2

5

11

2 4 3 7 5 8 6 10 9 11 1

Percolate down

39

Page 84: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

4

2

5

11

4 3 7 5 8 6 10 9 1112

deleteMin, write min element into empty cell

40

Page 85: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

6

3

7 8

910

4

5

11

4 3 7 5 8 6 10 9 111 2

deleteMin, write min element into empty cell

40

Page 86: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

6

7 8

910

4

3

5

4 6 7 5 8 11 10 9 123

Percolate down

41

Page 87: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

6

7 89

10

4

5

5 6 7 9 8 11 10 3 124

deleteMin, write min element into empty cell

42

Page 88: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

6

10 89

5

7

7 6 10 9 8 11 4 3 125

deleteMin, write min element into empty cell

43

Page 89: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

8

10 119

6

7

7 8 10 9 11 5 4 3 126

deleteMin, write min element into empty cell

44

Page 90: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

8

10 11

7

9

9 8 10 11 6 5 4 3 127

deleteMin, write min element into empty cell

45

Page 91: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

10

8

9

9 11 10 7 6 5 4 3 128

deleteMin, write min element into empty cell

46

Page 92: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

9

10

10 11 8 7 6 5 4 3 129

deleteMin, write min element into empty cell

47

Page 93: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

10

11

11 9 8 7 6 5 4 3 1210

deleteMin, write min element into empty cell

48

Page 94: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Heap Sort Example

11

10 9 8 7 6 5 4 3 1211

• Can use a max-heap if we want the output in increasing order.

49

Page 95: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• A classic divide-and-conquer algorithm.

• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

50

Page 96: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• A classic divide-and-conquer algorithm.

• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

50

Page 97: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort

1 21 32 512 8 34 64

• A classic divide-and-conquer algorithm.

• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51

Page 98: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort

1 21 32 512 8 34 64

1 2 8 21 32 34 51 64

• A classic divide-and-conquer algorithm.

• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51

Page 99: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 100: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 101: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1 2

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 102: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1 2 8

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 103: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1 2 8 21

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 104: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1 2 8 21 32

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 105: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr Bctr

Cctr

1 2 8 21 32 34

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 106: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

Actr

Cctr

1 2 8 21 32 34 51

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 107: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists

1 21 32 512 8 34 64

1 2 8 21 32 34 51 64

• Keep a pointers for each sub-list in the array.

• In each step, compare the elements they point two.

• If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.

• Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp

a

52

Page 108: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merging Sorted Sublists        private  static  <T  extends  Comparable<T>>          void  merge(  T[]  a,  T[]  tmpArray,  int  aCtr,  int  bCtr,  int  rightEnd  )  {                  int  leftEnd  =  bCtr  -­‐  1;                  int  tmpPos  =  aCtr;                  int  numElements  =  rightEnd  -­‐  aCtr  +  1;  

               //  Main  loop                  while(  aCtr  <=  leftEnd  &&  bCtr  <=  rightEnd  )                          if(  a[  aCtr  ].compareTo(  a[  bCtr  ]  )  <=  0  )                                  tmpArray[  tmpPos++  ]  =  a[  aCtr++  ];                          else                                  tmpArray[  tmpPos++  ]  =  a[  bCtr++  ];  

               while(  aCtr  <=  leftEnd  )        //  Copy  rest  of  first  half                          tmpArray[  tmpPos++  ]  =  a[  aCtr++  ];  

               while(  bCtr  <=  rightEnd  )    //  Copy  rest  of  right  half                          tmpArray[  tmpPos++  ]  =  a[  bCtr++  ];  

               //  Copy  tmpArray  back                  for(  int  i  =  0;  i  <  numElements;  i++,  rightEnd-­‐-­‐  )                          a[  rightEnd  ]  =  tmpArray[  rightEnd  ];          } 53

Page 109: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

54

Page 110: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

55

Page 111: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

56

Page 112: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

8 34 2 64 32 51 1 21

57

Page 113: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

8 34 2 64 32 51 1 21

2 8 34 64 1 21 32 51

58

Page 114: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort• Split the array in half, recursively sort each half.

• Merge the two sorted lists.

51 32 21 134 8 64 2

8 34 2 64 32 51 1 21

2 8 34 64 1 21 32 51

1 2 8 21 32 34 51 6459

Page 115: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort - Implementationprivate  static  <T  extends  Comparable<T>>  void  mergeSort(  T[]  a,  T[]  tmpArray,  int  left,  int  right  )    

       if(  left  <  right  )  {                  int  center  =  (  left  +  right  )  /  2;                  mergeSort(  a,  tmpArray,  left,  center  );                  mergeSort(  a,  tmpArray,  center  +  1,  right  );                  merge(  a,  tmpArray,  left,  center  +  1,  right  );          }  

}

60

Page 116: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort Running Time• This running time analysis is typical for divide and

conquer algorithms.

• Merge sort is a recursive algorithm. The running time analysis should be similar to what we have seen for other algorithms of this type (e.g. binary search)

• Base case: N=1 (sort a 1-element list). T(1) = 1

• Recurrence: T(N) = 2 T(N/2) + NRecursively sort each half

Merge the two halfs

61

Page 117: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort Running Time

assume

62

Page 118: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort Properties• Worst case running time:

• Is MergeSort stable?

• Space requirement?

63

Page 119: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort Properties• Worst case running time:

• Is MergeSort stable?

• Space requirement?

Yes. Merging preservers order of elements.

63

Page 120: Data Structures in Java - Columbia Universitybauer/cs3134-f15/slides/w3134-1-lecture14.pdf · Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1. Sorting Midterm

Merge Sort Properties• Worst case running time:

• Is MergeSort stable?

• Space requirement?

Yes. Merging preservers order of elements.

Need a temporary array. O(N)

63