Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort."— Presentation transcript:

1 Sorting Algorithms

2 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); }

3 CENG 213 Data Structures Sorting Algorithms There are many sorting algorithms, such as: –Insertion Sort –Bubble Sort –Merge Sort

4 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.

5 Original List After pass 1 After pass 2 After pass 3 After pass 4 After pass 5 23784583256 23784583256 23457883256 82345783256 82332457856 82332455678 SortedUnsorted

6 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; }

7 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.

8 Bubble Sort 23784583256 82378453256 82332784556 82332457856 82332455678 Original List After pass 1 After pass 2 After pass 3 After pass 4

9 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 }

10 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.

11 CENG 213 Data Structures Mergesort - Example

12 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); }

13 Mergesort - Example 63915 472 5 472 6391 6391 72 5 4 63195427 3619 27 4 5 2 457 1369 12345 789 divide merge

14 Mergesort – Example2

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

16 Linear Search Go through each element and try to find the target. For I from 0 to the n-1 if a[i] == target return i; return -1;

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

18 binary search: Locates a target value in a sorted array/list by successively eliminating half of the array from consideration. index012345678910111213141516 value-42710152022253036425056688592103 minmidmax

19 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 order public 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 }

20 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 index012345678910111213141516 value-42710152022253036425056688592103

21 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.

22 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"); }


Download ppt "Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort."

Similar presentations


Ads by Google