sorting algorithms. sorting sorting is a process that organizes a collection of data into either...

22
Sorting Algorithms

Upload: meagan-harrison

Post on 31-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Sorting Algorithms

Page 2: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Sorting

• Sorting is a process that organizes a collection of data into either ascending or descending order.

• public interface ISort {

void sort(Object[] items,int length);

}

Page 3: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

CENG 213 Data Structures

Sorting Algorithms

• There are many sorting algorithms, such as:– Insertion Sort– Bubble Sort– Merge Sort

Page 4: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Insertion Sort

• Insertion sort is a simple sorting algorithm that is appropriate for small inputs. – Most common sorting technique used by card players.

• The list is divided into two parts: sorted and unsorted.

• In each pass, the first element of the unsorted part is picked up, transferred to the sorted sublist, and inserted at the appropriate place.

• A list of n elements will take at most n-1 passes to sort the data.

Page 5: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Original List

After pass 1

After pass 2

After pass 3

After pass 4

After pass 5

23 78 45 8 32 56

23 78 45 8 32 56

23 45 78 8 32 56

8 23 45 78 32 56

8 23 32 45 78 56

8 23 32 45 56 78

Sorted Unsorted

Page 6: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Insertion Sort Algorithm

void insertionSort(Object[] a, int n){ for (int i = 1; i < n; i++) { Object tmp = a[i]; for (int j=i; j>0 && tmp < a[j-1]; j--) a[j] = a[j-1]; a[j] = tmp; }}

Page 7: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Bubble Sort• The list is divided into two sublists: sorted and

unsorted.• The smallest element is bubbled from the unsorted

list and moved to the sorted sublist.• After that, the wall moves one element ahead,

increasing the number of sorted elements and decreasing the number of unsorted ones.

• Each time an element moves from the unsorted part to the sorted part one sort pass is completed.

• Given a list of n elements, bubble sort requires up to n-1 passes to sort the data.

Page 8: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Bubble Sort23 78 45 8 32 56

8 23 78 45 32 56

8 23 32 78 45 56

8 23 32 45 78 56

8 23 32 45 56 78

Original List

After pass 1

After pass 2

After pass 3

After pass 4

Page 9: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Bubble Sort Algorithm void bubleSort(Object[] a, int n){ bool sorted = false; int last = n-1; for (int i = 0; (i < last) && !sorted; i++){ sorted = true; for (int j=last; j > i; j--) if (a[j-1] > a[j]{ swap(a[j],a[j-1]); sorted = false; // signal exchange } }}

Page 10: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Mergesort• Mergesort algorithm is one of two important divide-and-conquer

sorting algorithms (the other one is quicksort).• It is a recursive algorithm.

– Divides the list into halves, – Sort each halve separately, and – Then merge the sorted halves into one sorted array.

Page 11: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

CENG 213 Data Structures

Mergesort - Example

Page 12: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

CENG 213 Data Structures

Mergesort

void mergeSort(Object[] a, int n){mergeSortRecursive(a,0,n-1);

}void mergeSortRecursive(Object[] a, int first, int last) { if (first < last) { int mid = (first + last)/2; // index of midpoint mergeSortRecursive(a, first, mid); mergeSortRecursive(a, mid+1, last);

// merge the two halves merge(a, first, mid, last); }}

Page 13: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Mergesort - Example

6 3 9 1 5

4 7 2

5

4 7 26 3 9 1

6 3 9 1 7 25

4

6 3 19 5 4 27

3 6 1 9 2 74

5

2

4 5 71 3 6 9

1 2 3 4 5

78 9

divide

dividedividedivide

dividedivide

divide

merge merge

merge

merge

merge merge

merge

Page 14: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Mergesort – Example2

Page 15: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Search

public interface ISearch {int search(Object[] searchSpace,Object target);

}

Page 16: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Linear Search

Go through each element and try to find the target.

For I from 0 to the n-1if a[i] == target

return i; return -1;

Page 17: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Binary Search

• Binary search. Given value and sorted array a[], find index I

Page 18: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

• binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration.

index

0 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

value

-4 2 7 10

15

20

22

25

30

36

42

50

56

68

85

92

103

min mid max

Page 19: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Binary search code// Returns the index of an occurrence of target

in a,// or a negative number if the target is not

found.// Precondition: elements of a are in sorted

orderpublic static int binarySearch(int[] a, int

target) { int min = 0; int max = a.length - 1;

while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { min = mid + 1; } else if (a[mid] > target) { max = mid - 1; } else { return mid; // target found } }

return -(min + 1); // target not found}

Page 20: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Recursive binary search (13.3)

• Write a recursive binarySearch method.– If the target value is not found, return

its negative insertion point.

int index = binarySearch(data, 42); // 10

int index2 = binarySearch(data, 66); // -14

index

0 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

value

-4 2 7 10

15

20

22

25

30

36

42

50

56

68

85

92

103

Page 21: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

Exercise• Write three classes BubbleSort MergeSort and

InsertionSort. Let thoses classes implement the ISort interface.

• Write two classes LinearSort and BinarySearch which implemnent ISearch.

• Test your program with the given Test class.

Page 22: Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort

public class Test {

public static void main(String[] args) {

ISort sorter = new BubbleSort();

Object[] numbers1 = {3,5,2,72,48,23,1,43535,2342,1,34,6,42,423,42,4,453};

long startTime = System.currentTimeMillis();

sorter.sort(numbers1);

long stopTime = System.currentTimeMillis();

System.out.println("Bubble Sort takes "+(stopTime-startTime)+" ms");

sorter = new MergeSort();

Object[] numbers2 = {3,5,2,72,48,23,1,43535,2342,1,34,6,42,423,42,4,453};

startTime = System.currentTimeMillis();

sorter.sort(numbers2);

stopTime = System.currentTimeMillis();

System.out.println("Merge Sort takes "+(stopTime-startTime)+" ms");

sorter = new InsertionSort();

Object[] numbers3 = {3,5,2,72,48,23,1,43535,2342,1,34,6,42,423,42,4,453};

startTime = System.currentTimeMillis();

sorter.sort(numbers3);

stopTime = System.currentTimeMillis();

System.out.println("Insertion Sort Sort takes "+(stopTime-startTime)+" ms");

}

}