Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 60 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
60
Dung lượng
2,22 MB
Nội dung
Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM Sorting Algorithms Introduction As soon as you create a significant database, you’ll probably think of reasons to sort it in various ways Arrange names in alphabetical order, students by grade, customers by ZIP code, home sales by price, cities in order of increasing population, countries by GNP, stars by magnitude,… Sorting Algorithms Introduction A computer program can compare only two players at one time because that’s how the comparison operators work Two steps, executed over and over until the data is sorted: Compare two items Swap two items Sorting Algorithms Agenda Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Sorting Algorithms Bubble Sort Sorting Algorithms Bubble Sort Imagine that you’re like a computer program, so that you can see only two of the baseball players at the same time, if they’re next to each other and if you stand very close to them Let’s assume there are N players, and the positions they’re standing in are numbered from on the left to N-1 on the right Sorting Algorithms Bubble Sort You start at the left end of the line and compare the two kids in positions and If the one on the left (in 0) is taller, you swap them If the one on the right is taller, you don’t anything Then you move over one position and compare the kids in positions and Again, if the one on the left is taller, you swap them Sorting Algorithms Bubble Sort Here are the rules you’re following: Compare two players If the one on the left is taller, swap them Move one position right Sorting Algorithms Bubble Sort Sorting Algorithms Bubble Sort Sorting Algorithms Quicksort Why does it work? On the partition step algorithm divides the array into two parts and every element a from the left part is less or equal than every element b from the right part Also a and b satisfy a ≤ pivot ≤ b inequality After completion of the recursion calls both of the parts become sorted and, taking into account arguments stated above, the whole array is sorted Complexity analysis On the average quicksort has O(n log n) complexity In worst case, quicksort runs O(n2) time, but on the most "practical" data it works just fine and outperforms other O(n log n) sorting algorithms Sorting Algorithms Quicksort public void quickSort(int left, int right) { int index = findPartition(left, right); if (left < index - 1) quickSort(left, index - 1); if (index < right) quickSort(index, right); } Sorting Algorithms private int findPartition(int left, int right) { int i = left, j = right; T pivotElement = arr[(left + right) / 2]; while (i 0) j ; if (i l Find the middle point to divide the array into two halves: middle m = (l+r)/2 Call mergeSort for first half: mergeSort(arr, l, m) Call mergeSort for second half: mergeSort(arr, m+1, r) Merge the two halves sorted in step and 3: merge(arr, l, m, r) Sorting Algorithms public void mergeSort(int min, int max) { int left, right; // return if the list's lenghth = if (min == max) return; // find the length and the midpoint of the list int size = max - + 1; int pivot = (max + min) / 2; T[] tmp = (T[]) new Comparable[size]; // sort left half of the list mergeSort(min, pivot); mergeSort(pivot + 1, max); // copy sorted data to workspace for (int i = 0; i < tmp.length; i++) { tmp[i] = arr[min + i]; } Sorting Algorithms // merge the sorted list left = 0; right = pivot - + 1; for (int i = 0; i < tmp.length; i++) { if (right 0, selection sort makes (1/2)n(n – 1) key comparisons and 3(n – 1) item assignments For a list of length n, where n > 0, on average, insertion sort makes (1/4)n2 + O(n) ¼ O(n2) key comparisons and (1/4)n2 + O(n) ¼ O(n2) item assignments Let L be a list of n distinct elements Any sorting algorithm that sorts L by comparison of the keys only, in its worst case, makes at least O(nlog2n) key comparisons Sorting Algorithms Quick Review Both quicksort and mergesort sort a list by partitioning the list To partition a list, quicksort first selects an item from the list, called the pivot The algorithm then rearranges the elements so that the elements in one of the sublists are less than the pivot, and the elements in the second sublist are greater than or equal to the pivot In a quicksort, the sorting work is done in partitioning the list On average, the number of key comparisons in quicksort is O(nlog2n) In the worst case, the number of key comparisons in quicksort is O(n2) Mergesort partitions the list by dividing it in the middle In mergesort, the sorting work is done in merging the list The number of key comparisons in mergesort is O(nlog2n) Sorting Algorithms References Data Structure and Algorithms in Java Data Structure and Algorithms in C++ Wikipedia.org Sorting Algorithms Question? Sorting Algorithms ... players The item at the end of the array is sorted and won't be moved again Sorting Algorithms Example 8 8 8 8 5 8 (done) 8 13 Example Bubble Sort class ArrayBubbleSort {... }//end method Sorting Algorithms Agenda Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort Sorting Algorithms Selection Sort Sorting Algorithms Selection Sort Sorting Algorithms... elements) and swap it with the third element, and so on Sorting Algorithms Selection Sort Sorting Algorithms Example and analysis of Selection Sort 8 4 8 7 31 The Selection Sort might swap an array