+ All Categories
Home > Documents > 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy...

1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy...

Date post: 04-Jan-2016
Category:
Upload: osborne-walton
View: 218 times
Download: 0 times
Share this document with a friend
Popular Tags:
18
1 2. Program Construction in Java
Transcript
Page 1: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

1

2. Program Construction in Java

Page 2: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

2.9 Sorting

Page 3: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

3

The need

•Soritng into categories is relatively easy (if, else if, switch); here we consider sorting into an order.

•Sorting a database by certain criteria is important for displaying the data and essential for processing e.g. binary search.

•If necessary, sorting can be performed at times when the system is quiet e.g. overnight.

Page 4: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

4

The need

•SL and HL require 2 sorting algorithms:

‣Selection sort and

‣Bubble sort.

‣ [we will also look at insertion sorting]

•HL requires

‣Quicksort.

Page 5: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

5

Insertion sort

•The basic algorithm:

‣ take the array (called e.g. unsorted []) and create another (e.g. sorted[]) of the same type/size;

‣ scan unsorted for its lowest value and move it to the first available position in sorted;

‣ continue until the whole array is in sorted (in the correct order).

Page 6: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

6

Insertion sort

•Two arrays (5 elements):

‣ int [] unsorted = {6,12,2,9,8};int [] sorted = new int [5];

•Suppose we have already written a method called public int getMinValuePos ()which returns the smallest value’s position in the unsorted array.

Page 7: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

7

Insertion sort

•In a for loop, find where the minimum is and move it into sorted, deleting the old.

‣ for (int i=0; i<=4; i++){ minPos = getMinValuePos (); sorted [i] = unsorted [minPos]; unsorted [minPos] = 1001;}

Page 8: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

8

Insertion sort

•ArrayInsertionSort demonstrates this method.

•However, there is a more effecient way...

Page 9: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

9

Selection sort

•Basic algorithm:

‣divide the one array in two, one part sorted and the other unsorted;

‣ scan the unsorted part for its lowest value and move it to the next available position in the sorted part;

‣ continue until whole array is sorted.

Page 10: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

10

Selection sort

•One array (5 elements):

‣ int [] numbers = {6,12,2,9,8};

•Now we need a getMinValue(start) method which can return the smallest value in the unsorted part from a moving starting point to the end.

Page 11: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

11

Selection sort

•Note the selection sort calls the method getMinValue (int, int, int) to find the minimum value each time it is needed i.e. it is OK to call a method from within a method from within a method

•The method getMinValue has been written separately in this example.

Page 12: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

12

Bubble sort 1

•Basic algorithm:

‣ look at successive pairs;

‣ if they need swapping, swap them;

‣ repeat for the next pair;

‣ sweep through the array as many times as there are elements.

Page 13: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

13

Bubble sort 1

•See this implemented in BubbleSort1

• N.B. Net effect of this one pass is only to 'bubble' the highest to the top

•N.B. In fact only12 comparisons are made for a 13 element array. Why, when there are 13 items?

Page 14: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

14

Bubble sort 2

•BubbleSort2 uses nested loops ( a while within a for ), so the array is traversed many times.

•All values bubble to their correct position.

•The number of traversals is actually one less than the number of items in the array.

Page 15: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

15

Bubble sort 2

•Beware of the index out of bounds error - you have gone beyond the end of the array.

•If you have n items to sort, you only need to put n-1 in place and the last one will automatically be in the correct position.

Page 16: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

16

Bubble sort 3

•There is no point continuing the sort if everything is in place.

•The program will not know to stop without a test that it has already finished in the code and so would spend just as long "sorting" a perfectly ordered array as it would a jumbled one.

Page 17: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

17

Bubble sort 3

•In BubbleSort3, a boolean variable called swapped (known as a flag ) is used to keep track of whether anything was swapped on the last pass through the array.

•If it was not, all are in order and the sorting can terminate early.

Page 18: 1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.

18

Bubble sort 4

•Since the first pass bubbles the largest to the top, there is no need to check the top value on the second pass. Similarly for the second largest on the third pass, etc.

•BubbleSort4 stops the pass through the array one short each time.


Recommended