COP 3503 FALL 2012 Shayan Javed Lecture 16

# COP 3503 FALL 2012 Shayan Javed Lecture 16

## COP 3503 FALL 2012 Shayan Javed Lecture 16

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. COP 3503 FALL 2012ShayanJavedLecture 16 Programming Fundamentals using Java

2. Sorting • Another crucial problem. • Used everywhere: • Sorting numbers (prices/grades/ratings/etc..) • Names • Dates • Rating

3. Sorting • When shopping online (Amazon for ex.):

4. Sorting • We designed a RedBox system where you can sort by different criteria

5. Sorting • We designed a RedBox system where you can sort by different criteria • How can you sort in Java?

6. Sorting • We designed a RedBox system where you can sort by different criteria • How can you sort in Java? • Arrays.sort(..) – but how does it work?

7. Sorting • We designed a RedBox system where you can sort by different criteria • How can you sort in Java? • Arrays.sort(..) – but how does it work? • Going to look at a few different algorithms.

8. Sorting • Let’s see if we can come up with one on our own...

9. Sorting • Let’s see if we can come up with one on our own... • array A (N = A.length): • sorted array A:

10. Sorting • Compare values at indices 0 and 1 first.

11. Sorting • Compare values at indices 0 and 1 first.

12. Sorting • Compare values at indices 0 and 1 first. • Swap if A[1] < A[0]:

13. Sorting • Compare values at indices 1 and 2.

14. Sorting • Compare values at indices 1 and 2. • Is A[2] < A[1]? No:

15. Sorting • Compare values at indices 2 and 3.

16. Sorting • Compare values at indices 2 and 3. • Is A[3] < A[2]? Yes! Swap.

17. Sorting • Keep repeating this process until you reach the end of the array.

18. Sorting • Keep repeating this process until you reach the end of the array. • Result:

19. Sorting • Largest value in the array is now at the end of the array. • Result:

20. Sorting • What do we do next?

21. Sorting • What do we do next? • Repeat the process.

22. Sorting • What do we do next? • Repeat the process. • Result:

23. Sorting • Have the second largest value at index N - 2 • Result:

24. Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Result:

25. Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Total of N times • Result:

26. Sorting • Have the second largest value at index N - 2 • How many more times to sort? • Total of Ntimes • Result:

27. Bubble Sort • This algorithm is known as “Bubble Sort”

28. Bubble Sort • This algorithm is known as “Bubble Sort” • The max value “bubbles” to the end of the list at each pass.

29. Bubble Sort • This algorithm is known as “Bubble Sort” • The max value “bubbles” to the end of the list at each pass. • Simplest sorting algorithm.

30. Bubble Sort • Let’s write the code for it:

31. Bubble Sort • Let’s write the code for it: static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

32. Bubble Sort • Let’s write the code for it: static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

33. Bubble Sort • Let’s write the code for it: static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

34. Bubble Sort • Let’s write the code for it: static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

35. Bubble Sort • Let’s write the code for it: static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

36. Bubble Sort Efficiency • How efficient is it?

37. Bubble Sort Efficiency • How efficient is it? • What is the O(..) of the algorithm?

38. Bubble Sort Efficiency • Let’s look at one pass.

39. Bubble Sort Efficiency • Let’s look at one pass.

40. Bubble Sort Efficiency • Let’s look at one pass. • To:

41. Bubble Sort Efficiency • Let’s look at one pass. • To: • What’s the efficiency of this pass?

42. Bubble Sort Efficiency • Let’s look at one pass. • To: • What’s the efficiency of this pass? O(N)

43. Bubble Sort Efficiency • How many of these passes do we have?

44. Bubble Sort Efficiency • How many of these passes do we have? • N passes.

45. Bubble Sort Efficiency • How many of these passes do we have? • N passes. • Efficiency: O(N) * N = O(N2)

46. Bubble Sort Efficiency • How can we make the algorithm a little more efficient/faster?

47. Bubble Sort Efficiency static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < (N-i); j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } }

48. Bubble Sort Efficiency static void bubbleSort(int[] A, int N) { for(inti = 0; i < N; i++) { for (int j = 1; j < (N-i); j++) { if (A[j] < A[j-1]) { // swap int temp = A[j]; A[j] = A[j-1]; A[j-1] = temp; } } } } We know the end keeps getting sorted properly

49. Bubble Sort Efficiency • But efficiency is still O(N2)

50. Bubble Sort Efficiency • But efficiency is still O(N2) • Thus bubble sort isn’t really a good sorting algorithm...