Data Structures(数据结构) Course 11: Advanced Sorting

Slides:



Advertisements
Similar presentations
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Advertisements

Sorting Chapter 8 CSCI 3333 Data Structures.
Lesson Plan - 2: Bubble Sort, Quick Sort
Ceng-112 Data Structures I Chapter 11 Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Chapter 19: Searching and Sorting Algorithms
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CHAPTER 11 Sorting.
C++ Plus Data Structures
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
Sorting Chapter 10.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting Chapter 12 Objectives Upon completion you will be able to:
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Data Structures: A Pseudocode Approach with C, Second Edition1 Chapter 12 Objectives Upon completion you will be able to: Understand the basic concepts.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
UNIT-III SORTING Lesson Plan-1 Insertion Sort, Selection Sort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
1 Chapter 8 Sorting. 2 OBJECTIVE Introduces: Sorting Concept Sorting Types Sorting Implementation Techniques.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Searching and Sorting Searching algorithms with simple arrays
Sorting.
Sorting.
Algorithm Efficiency and Sorting
Chapter 2 (16M) Sorting and Searching
Data Structures Using C++ 2E
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Advanced Sorting Methods: Shellsort
SORTING, SEARCHING AND HASHING TECHNIQUES
Algorithm Efficiency and Sorting
CSE 373 Data Structures and Algorithms
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Algorithm Efficiency and Sorting
Advanced Sorting Methods: Shellsort
Presentation transcript:

Data Structures(数据结构) Course 11: Advanced Sorting

Vocabulary Sort (排序) Internal sort (内排序) External sort (外排序) Ascending order (升序) Descending order (降序) Insertion sort (插入排序) Straight insertion sort (直接插入排序) Shell sort (希尔排序) Selection sort (选择排序) Straight selection sort (直接选择排序) Heap sort (堆排序) Exchange sort (交换排序) Bubble sort (冒泡排序) Quick sort (快速排序) Stability (稳定性) Pass (一趟排序)

Highlights in this chapter General sort concept Sort order Sort stability Sort efficiency passes Insertion sorts Selection sorts Exchange sorts

11-1 General sort concept Internal sort External sort Sort order An attribute of sort indicating that data with equal keys maintain their relative input order in the output. 365 blue 212 green 876 white 212 yellow 119 purple 737 green 212 blue 443 red 567 yellow Unsorted data Stable data Unstable data Internal sort External sort Sort order Sort stability Sort efficiency passes All of data are held in primary memory during the sorting process. It are classified as : Insertion Sort Selection sort Exchange sort Merging sort Distribution sort Uses primary storage (内存) for the data currently being sorting and secondary storage (外存) for any data that will not fit in primary memory. Data may be sorted in either ascending or descending order. The sort order identifies the sequence of the data A measure of the relative efficiency of a sort. It is usually an estimate of the number of comparisons and moves required to order an unordered list. Each traversal of the during the sorting process.

11-2 Insertion sorts Insertion sort: in each pass of an insertion sort, one or more pieces of data are inserted into their correct location in an order list. In this section we study two insertion sorts, the straight insertion sort and the shell sort.

Straight insertion sorts Algorithm insertionSort ( ref list <array>, val last < index >) Sort list[0…last] using insertion sort. The array is divided into sorted and unsorted lists. With each pass, the first element in the unsorted list is inserted into the sorted list. Pre list must contain at least one element last is an index to last element in the list Post list has been rearranged Current = 1 Loop (Current <= last) 1 hold = list[Current ] 2 walker = Current - 1 3 loop (walker >= 0 and hold.key < list[walker].key) 1 list[walker + 1] = list[walker] 2 walker = walker – 1 4 end loop 5 list[walker + 1] = hold 6 current = current + 1 end loop return End insertionSort Straight insertion sorts 23 78 45 8 32 56 Original list Unsorted In the straight insertion sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the first element of the unsort sublist is transferred to the sorted sublist by inserting it at the appropriate place. To improve the efficiency,we use a hold area. Unsorted 23 78 45 8 32 56 After pass 1 Sorted Unsorted 23 45 78 8 32 56 After pass 2 Sorted Inner loop: start with high end of the sorted list and work toward the beginning of the sorted area. Unsorted 8 23 45 78 32 56 After pass 3 Sorted Straight insertion sort example Algorithm insertionSort 8 23 32 45 78 56 After pass 4 Sorted 8 23 32 45 56 78 After pass 5 Sorted

A pass: (k = 3) after pass through the data, the data in three segments are ordered 1 compare element A[0] and A[3], if out of sequence then changed 2 compare element A[1] and A[4], if out of sequence then changed 3 compare element A[2] and A[5], if out of sequence then changed 4 compare element A[3] and A[6], if out of sequence then changed 5 compare element A[4] and A[7], if out of sequence then changed 6 compare element A[5] and A[8], if out of sequence then changed 7 compare element A[6] and A[9], if out of sequence then changed Shell sorts The shell sorts is an improve version of the straight insertion sort, in which diminishing partition are used to sort data. The segments in a shell sort: A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[0+k] A[0+2*k] A[0+3*k] K=3 Segment 1 A[1+k] A[1+2*k] A[2+K] A[2+2*k] increment

Diminishing increments in shell sort (c) Third increment : k = 1 14 21 9 25 55 30 70 80 62 77 (b) second increment : k = 2 21 62 14 9 30 77 80 25 70 55 77 62 14 9 30 21 80 25 70 55 (a) First increment : k = 5

algorithm Shellsort incre = last / 2 Loop (incre not 0) 1 Current = incre 2 Loop (Current <= last) 1 hold = list[Current ] 2 walker = Current - incre 3 loop (walker >= 0 and hold.key < list[walker].key) 1 list[walker + incre] = list[walker] 2 walker = walker – incre 4 end loop 5 list[walker + incre] = hold 6 current = current + 1 3 end loop 4 incre = incre / 2 End loop return End ShellSort Algorithm ShellSort ( ref list <array>, val last < index >) Data in list[0], list[1], …, list[last] are sorted in place. After the sort, their keys will be in order, list[0] <= list[1]<= … <= list[last]. Pre list is an unsorted array of records last is an index to last record in array Post list is ordered on list[i].key To reduce the number of elements that appear in multiple segments, we must consider the increment of shell sort. The better is use the prime number, but in most cases is use the simple series we proposed in this algorithm

insertion sort algorithm efficiency The out loop executes n-1 times. The inner loop executes from 0 to current times. Straight insertion sort 2 Loop (Current <= last) …… 3 loop (walker >= 0 and hold.key < list[walker].key) The out loop is logarithmic, it excutes it executes log2n times. The first inner loop executes n-increment times. The innermost loop is the most difficult to analyze. f(n) =( n-1) *(n+1)/2 In big-O notation: O(n2 ) Shell sort 2 Loop (incre not 0) 1 Current = incre 2 Loop (Current <= last) …… 3 loop (walker >= 0 and hold.key < list[walker].key) In big-O notation: O(n1.25 )

11-3 Selection sorts Selection sort: In each pass of an selection sort, we select the smallest item of unsorted list and place it in the sorted list. In this section we study two selection sorts, the straight selection sort and the heap sort.

Straight selection sorts Algorithm selectionSort ( ref list <array>, val last < index >) Sort list[0…last] by selecting smallest element in unsorted portion of array and exchange it with element at the beginning of the unsorted list. Pre list must contain at least one element last is an index to last element in the list Post list has been rearranged smallest to largest Current = 0 Loop (Current < last) 1 smallest = Current 2 walker = Current + 1 3 loop (walker <= last) 1 if (list[walker] < list[smallest]) 1 smallest = walker 2 walker = walker + 1 4 end loop 5 exchange (list, current, smallest) 6 current = current + 1 end loop return End selectionSort Straight selection sorts 23 78 45 8 32 56 Original list Unsorted In the straight selection sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, we select the smallest element from the unsorted sublist and exchange it with the element at the beginning of the unsort data. Unsorted 8 78 45 23 32 56 After pass 1 Unsorted 8 23 45 78 32 56 After pass 2 Sorted Straight selection sort example Unsorted 8 23 32 78 45 56 After pass 3 Sorted Algorithm selectionSort 8 23 32 45 78 56 After pass 4 Sorted Unsorted 8 23 32 45 56 78 After pass 5 Sorted

selection sort algorithm efficiency Straight selection sort The out loop executes n-1 times. The inner loop also executes n/2 times. 2 Loop (Current < last) …… 3 loop (walker <= last) big-O notation: O(n2 )

11-4 Exchange sorts Exchange sort: In each pass of an exchange sort, we exchange elements that are out of order, until the entire list is sorted. In this section we study two exchange sorts, the bubble sort and the most efficient general-purpose sort quick sort.

Bubble sorts Bubble sort example Algorithm bubbleSort Algorithm bubbleSort ( ref list <array>, val last < index >) Sort an array, list[0…last], using bubble sort. Adjacent element are compared and exchanged until list is completely ordered. Pre list must contain at least one element last is an index to last element in the list Post list has been rearranged smallest to largest Current = 0 Sorted = false Loop (Current < last and sorted false) 1 walker = last 2 sorted = true 3 loop (walker > current) 1 if (list[walker] < list[walker - 1]) 1 sorted = false 2 exchange(list, walker, walker – 1) 2 end if 3 walker = walker - 1 4 end loop 5 current = current + 1 end loop return End bubbleSort Bubble sorts 23 78 45 8 56 32 Original list Unsorted In the bubble sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the smallest element is bubbled from the unsorted sublist and moved to the sorted sublist. Each iteration is one sort pass. Unsorted 8 23 78 45 32 56 After pass 1 If any exchange is not made in a passs, then we know the list is already sorted and the sort can stop. Any exchange means list is not sorted. Unsorted 8 23 32 78 45 56 After pass 2 Sorted Bubble sort example Unsorted 8 23 32 45 78 56 After pass 3 Sorted Algorithm bubbleSort 8 23 32 45 56 78 After pass 4 Sorted Unsorted

Quick sorts Quick sort partitions After first partitioning Keys < pivot Keys >= pivot pivot In the Quick sort, we select an element as pivot, the list is divided into three group: a partition of elements whose keys are less than the pivot’s key, the pivot element that is place in correct location, and a partition of elements greater than or equal pivot’s key. The sorting then continues by quick sorting the left partition. If the left partition is sorted, then quick sorting the right partition. After second partitioning < pivot >= pivot pivot After third partitioning Sorted After fourth partitioning Sorted After fifth partitioning Sorted < pivot >= pivot pivot Quick sort partitions After sixth partitioning Sorted After seventh partitioning Sorted

Algorithm quickinsertionSort ( ref list <array>, val fist < index >, val last < index >) Pre list must contain at least one element first is an index to first element in the list last is an index to last element in the list Post list has been rearranged Current = first + 1 Loop (Current <= last) 1 hold = list[Current ] 2 walker = Current - 1 3 loop (walker >= first and hold.key < list[walker].key) 1 list[walker + 1] = list[walker] 2 walker = walker – 1 4 end loop 5 list[walker + 1] = hold 6 current = current + 1 end loop return End insertionSort Quick Sorts Algorithm Algorithm medianLeft ( ref sortDatat <array>, val left < index >, val right < index >) Pre sortDatat is an array of at least three elements left and right are the boundaries of array Post median value located and place at sortData[left] mid = (left + right) / 2 If (sortData[left].key > sortData[mid].key) 1 exchange (sortData, left, mid) end if 4 If (sortData[left].key > sortData[right].key) 1 exchange (sortData, left, right) 5 end if 6 If (sortData[mid].key > sortData[right].key) 1 exchange (sortData, mid, right) Median is in middle location, exchange with left. exchange (sortData, left, mid) 9 return End medianLeft In addition to basic algorithm, two supporting algorithm are required: determine the pivot, and the straight insertion sort. Straight insertion module Sort list[first…last] using insertion sort. The list is divided into sorted and unsorted lists. With each pass, the first element in the unsorted list is inserted into the sorted list. This is a special version of the insertion sort modified for use with quick sort. determinie median of three Find the median value of an array, sortData[left…right], and place it in the location sortData[left].

Quick Sorts Algorithm (continue) Original data 78 21 14 97 87 62 74 85 76 45 84 22 Quick Sorts Algorithm (continue) 62 21 14 97 87 78 74 85 76 45 84 22 22 21 14 97 87 78 74 85 76 45 84 62 Quick sort pivot 22 21 14 97 87 62 74 85 76 45 84 78 78 21 14 97 87 62 74 85 76 45 84 22 62 22 21 14 45 87 74 85 76 97 84 78 22 14 21 45 14 21 78 76 74 85 97 84 87 74 76 84 85 97 84 85 87 Exchange 62 21 14 97 87 22 74 85 76 45 84 78 97 sortLeft sortRight 45 62 Quick sort operation 62 21 14 45 87 62 74 85 76 97 84 78 87 sortLeft sortRight 22 Exchange 62 62 21 14 45 22 87 74 85 76 97 84 78 sortRight sortLeft Move 62 22 21 14 45 62 87 74 85 76 97 84 78 < pivot 62 pivot >= pivot

Quick Sorts Algorithm (continue) Algorithm quickSort ( ref list <array>, val left < index >, val right < index >) If (( right – left ) > minSize 1 medianLeft (list, left, right) 2 pivot = list[left] 3 sortLeft = left + 1 4 sortRight = right 5 loop ( sortLeft <= sortright) 1 loop (list[sortLeft].key < pivot.key) 1 sortLeft = sortLeft + 1 2 end loop 3 loop (list[sortRight].key >= pivot.key 1 sortRight = sortRight – 1 4 end loop 5 if (sortLeft <= sortRight ) 1 exchange (list, sortLeft, sortRight ) 2 sortLeft = sortLeft +1 3 sortRight = sortRight –1 6 end if 6 end loop Quick Sorts Algorithm (continue) Quick sort algorithm: quickSort An array ,list [left…right] is sorted using recursion. Pre list is an array of data to be sorted left and right identify the first and last elements of the list, respectively Post list is sorted 7 list [left] = list [sortLeft - 1] 8 list [sortLeft - 1] = pivot 9 if (left < sortRight ) 1 quickSort (list, left, sortRight - 1) 10 end if 11 if (sortLeft < right ) 1 quickSort (list, sortLeft, right) 12 end if Else 1 insertionSort (list, left, right) 3 end if End quickSort

Exchange Sorts Algorithm efficiency Bubble sort 3 Loop (Current <= last and sorted false) …… 3 loop (walker > Current ) f(n) = n (n+1)/2 In big-O notation: O(n2 ) 5 Loop (Current <= sortRight) …… 1 loop (list[sortLeft].key < pivot.key) 3 loop (list[sortRight].key >= pivot.key 6 end loop Quick sort f(n) = n (log2n + log2n) =2nlog2n, In big-O notation: O(nlog2n)

Summary of this chapter One of the most common application in computer science is sorting. Sorts are generally classified as either internal or external. In an internal sort, all of the data are held in primary storage during the sorting process. An external sort uses primary storage for the data currently being sorting and secondary storage for any data that will not fit in primary memory. Data may be sorted in either ascending or descending order. Sort stability is an attribute of sort indicating that data with equal keys maintain their relative input order in the output. Sort efficiency is a measure of the relative efficiency of a sort. Each traversal of the during the sorting process is referred to as a pass. Internal sorting can be divided into three broad categories: insertion, selection, and exchange.

Summary of this chapter (Continue) Two methods of insertion sorting were discussed in this chapter: straight insertion sort and shell sort. In the straight insertion sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the first element of the unsort sublist is transferred to the sorted sublist by inserting it at the appropriate place. The shell sort algorithm is an improved version of the straight insertion sort, in which the process uses difference increments to sort the list. In each increment, the list is divided into segments, which are sorted independent of each other. Two methods of selection sorting were discussed in this chapter: straight selection sort and heap sort. In the straight selection sort, the list at any moment is divided into two sublist: sorted and unsorted. In each pass, the process selects the smallest element from the unsorted sublist and exchange it with the element at the beginning of the unsorted sublist.

Summary of this chapter (Continue) Two methods of exchange sorting were discussed in this chapter: bubble sort and quick sort. In the bubble sort, the list at any moment is divided into two sublists: sorted and unsorted. In each pass, the smallest element is bubble up from the unsorted sublist and moved to the sorted sublist. The quick sort is the new version of exchange sort in which the list is continuously divided into smallest sublists and exchange takes place between elements that are out of order. Each pass of the quick sort selects a pivot and divides list into three groups: a partition of elements whose key is less than the pivot element that is place in its ultimate correct position, and a partition of elements greater than or equal to the pivot’s key. The sorting then continues by quick sorting the left partition followed by quick sorting the right partition. The efficiency of straight insertion, straight selection, and bubble sort is O(n2). The efficiency of shell sort is O(n1.25), and The efficiency of quickl sort is O(nlog2n).

Exercises An array contains the elements shown below. The first two elements have been sorted using a straight insertion sort, what would be the value of the elements in the array after three more passes of the straight insertion sort algorithm? 3,13,7,26,44,23,98,57 An array contains the elements shown below. Show the contents of the array after it has gone through a one-increment of the shell sort. The increment factor K=3 23,3,7,13,89,7,66,2,6,44,18,90,98,57 An array contain the elements shown below. The first two elements have been sorted using a straight selection sort. What would be the value of the elements in the array after three more passes of the selection sort algorithm? 7,8,26,44,13,23,98,57

Exercises An array contains the elements shown below. using a quick sort show the contents of the array after the first pivot has been placed in its correct location. Identify the three sublists that exist at that point . 44,78,22,7,98,56,34,2,38,35,45 After two passes of a sorting algorithm ,the following array: 47,3,21,32,56,92 has been rearranged as shown below. 3,21,47,32,56,92 which sorting algorithm is being used.(straight insertion, buble, straight selection)

Exercises After two passes of a sorting algorithm ,the following array: 80,72,66,44,21,33 has been rearranged as shown below. 21,33,80,72,66,44 which sorting algorithm is being used. .(straight insertion, buble, straight selection) 47,3,66,32,56,92 has been rearranged as shown below. 3,47,66,32,56,92

Homework Realize straight insertion and straight selection algorithm using array data shown below. 7,8,26,44,13,23,98,57