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

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 Search public interface ISearch { int search(Object[] searchSpace,Object target); }

11 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;

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

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

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

15 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


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