Algorithms III

Total Page:16

File Type:pdf, Size:1020Kb

Algorithms III Algorithms III 1 Agenda Sorting • Simple sorting algorithms • Shellsort • Mergesort • Quicksort • A lower bound for sorting • Counting sort and Radix sort Randomization • Random number generation • Generation of random permutations • Randomized algorithms 2 Sorting Informal definition: Sorting is any process of arranging items in some sequence. Why sort? (1) Searching a sorted array is easier than searching an unsorted array. This is especially true for people. Example: Looking up a person in a telephone book. (2) Many problems may be solved more efficiently when input is sorted. Example: If two files are sorted it is possible to find all duplicates in only one pass. 3 Detecting duplicates in an array If the array is sorted, duplicates can be detected by a linear-time scan of the array: for (int i = 1; i < a.length; i++) if (a[i - 1].equals(a[i]) return true; 4 Finding the intersection of two! unsorted arrays 0 i M a: 0 j N b: 0 k c: k = 0; for (i = 0; i < M; i++) for (j = 0; j < N; j++) if (a[i].equals(b[j])) c[k++] = a[i]; Complexity: O(MN) 5 Finding the intersection of two! sorted arrays 0 i M a: 0 j N b: 0 k c: i = j = k = 0; while (i < M && j < N) if (a[i] < b[j]) i++; else if (a[i] > b[j]) j++; else { c[k++] = a[i]; i++; j++; } Complexity: O(M + N) 6 Insertion sort Problem: Given an array a of n elements a[0], a[1], ..., a[n-1]. Sort the array in increasing order. Solution (by induction): Base case: We know how to sort 1 element. Induction hypothesis: We know how to sort n-1 elements. We can sort an array of n elements by (1) sorting its first n-1 elements, (2) insert the n’th element into its proper place among the previously sorted elements 7 Insertion sort (recursive version) void insertionSort(int[] a, int n) { if (n > 1) { insertionSort(a, n - 1); int tmp = a[n]; int j = n; for ( ; j > 0 && a[j - 1] > tmp; j--) a[j] = a[j - 1]; a[j] = tmp; } } Single right shift 8 9 10 A S O R T I N G E X A M P L E A S O R T I N G E X A M P L E A O S R T I N G E X A M P L E A O R S T I N G E X A M P L E A O R S T I N G E X A M P L E A I O R S T N G E X A M P L E A I N O R S T G E X A M P L E A G I N O R S T E X A M P L E A E G I N O R S T X A M P L E A E G I N O R S T X A M P L E A A E G I N O R S T X M P L E A A E G I M N O R S T X P L E A A E G I M N O P R S T X L E A A E G I L M N O P R S T X E A A E E G I L M N O P R S T X 11 Insertion sort (non-recursive version) Only the object references are right-shifted (not the objects themselves) 12 Animation of Insertion sort a[i] i Sorting the integers from 1 to 100. 13 Analysis of Insertion sort Number of comparisons: Best case: n-1 Worst case: 1 + 2 + ... + (n-1) = n(n-1)/2, which is O(n2) Average case: n(n-1)/4, which is O(n2) Number of moves: Best case: 0 Worst case: 1 + 2 + ... + (n-1) = n(n-1)/2, which is O(n2) Average case: n(n-1)/4, which is O(n2) Running time is O(n) for “almost sorted” arrays 14 Shellsort! Donald Shell, 1959 Idea: Insertion sort is very efficient when the array is “almost sorted”. However, it is slow for “very unsorted” arrays since it only allows exchange of neighboring elements. Question: Can we start by exchanging elements that are far apart from each other in the array before we do an ordinary Insertion sort? Answer: Yes. We can start by sorting all subfiles consisting of every h’th element of the array, where h > 1. 15 4-sorting 1. Divide the array into 4 subfiles: each 4’th element, starting in the first, each 4’th element, starting in the second, each 4’th element, starting in the third, and each 4’th element, starting in the fourth. 2. Sort each of these four subfiles. The array is then said to be 4-sorted. Similarly, we can define h-sorted. Notice that an array that is 1-sorted is sorted. 16 4-sorting Use Insertion sort with an increment of 4 A S O R T I N G E X A M P L E A S O R T I N G E X A M P L E A I O R T S N G E X A M P L E A I N R T S O G E X A M P L E A I N G T S O R E X A M P L E A I N G E S O R T X A M P L E A I N G E S O R T X A M P L E A I A G E S N R T X O M P L E A I A G E S N M T X O R P L E A I A G E S N M P X O R T L E A I A G E L N M P S O R T X E A I A G E L E M P S N R T X O 17 Implementation of h-sort public static <AnyType extends Comparable<? super AnyType>> void h_sort(AnyType[] a, int h) { for (int p = h; p < a.length; p++) { AnyType tmp = a[p]; int j = p; for ( ; j >= h && tmp.compareTo(a[j - h]) < 0; j -= h) a[j] = a[j - h]; a[j] = tmp; } } The code is obtained by replacing 1 by h in insertionSort. 18 Shellsort Shellsort uses h-sorting for a decreasing sequence of h-values, ending with h = 1. void shellsort(AnyType[] a) { for (int h = a.length / 2; h > 0; h = h == 2 ? 1 : (int) (h / 2.2)) h_sort(a, h); } 19 20 In the figure, elements spaced five and three apart are identically shaded. 21 Animation of Shellsort 22 (milliseconds) Shell’s increments: Start gaps at n/2 and halve until 1 is reached. Odd Gaps Only: As Shell’s increments, but add 1 whenever the gap becomes even. 23 Analysis of Shellsort Number of comparisons (for the increment sequence 1, 4, 13, 40,...): Best case: (n-1) + (n-4) + (n-13) + ... ≤ n log3n Worst case: less than n1.5 Average case: not known Two suggestions are O(n1.25) and O(n(log n)2) Number of moves: Best case: 0 Worst case: as for comparisons Average case: as for comparisons 24 Bubble Sort Repeatedly swap adjacent elements if they are in wrong order. boolean swapped; do { swapped = false; for (int i = 1; i < a.length; i++) { if (a[i - 1] > a[i]) { swap(a, i - 1, i); swapped = true; } } } while (swapped); Worst-case and average complexity: O(n2) Best case complexity: O(n) 25 Improved Bubble Sort All elements after the last swap are sorted, and do not need to be checked again. int n = a.length; do { int newn = 0; for (int i = 1; i < n; i++) { if (a[i - 1] > a[i]) { swap(a, i - 1, i); newn = i; } } n = newn; } while (n > 0); Worst-case and average complexity: O(n2) Best case complexity: O(n) 26 Mergesort Mergesort is a divide-and-conquer based sorting algorithm. The algorithm involves three steps: 1. If the number of items to sort is 0 or 1, then return. 2. Recursively sort the first and second halves separately. 3. Merge the two sorted halves into one sorted group. 27 Mergesort void mergeSort(Comparable[] a, int low, int high) { if (low < high) { int mid = (low + high) / 2; mergesort(a, low, mid); mergesort(a, mid + 1, high); merge(a, low, mid, high); } } ≤ ≤ low mid high 28 Linear-time merging of two sorted arrays 0 i M a: ≤ ≤ 0 j N b: ≤ ≤ 0 k M+N c: ≤ for (i = j = k = 0; k < M + N; k++) if (i == M) c[k] = b[j++]; else if (j == N) c[k] = a[i++]; else c[k] = a[i].compareTo(b[j]) < 0 ? a[i++] : b[j++]; 29 Merging a[low:mid] with a[mid+1:high] to a[low:high]: a: ≤ ≤ low mid mid+1 high Merge into b: b: ≤ low high Copy b to a: a: ≤ low high void merge(Comparable[] a, int low, int mid, int high) { int i = low, j = mid + 1; for (int k = low; k <= high; k++) if (i > mid) b[k] = a[j++]; else if (j > high) b[k] = a[i++]; else b[k] = a[i].compareTo(a[j]) < 0 ? a[i++] : a[j++]; for (int k = low; k <= high; k++) a[k] = b[k]; } 30 31 32 A S O R T I N G E X A M P L E A S O R T I N G E X A M P L E A S O R T I N G E X A M P L E A O R S T I N G E X A M P L E A O R S I T N G E X A M P L E A O R S I T G N E X A M P L E A O R S G I N T E X A M P L E A G I N O R S T E X A M P L E A G I N O R S T E X A M P L E A G I N O R S T E X A M P L E A G I N O R S T A E M X P L E A G I N O R S T A E M L L P E A G I N O R S T A E M L E L P A G I N O R S T A E E L M P X A A E E G I L M N O P R S T X 33 Animation of Mergesort 34 Call trees 16 8 8 4 4 4 4 2 2 2 2 2 2 2 2 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 21 10 11 5 5 5 6 2 3 2 3 2 3 3 3 1 1 1 2 1 1 1 2 1 1 1 2 1 2 1 2 1 1 1 1 1 1 1 1 1 1 35 Call tree There are about log2N levels Each level uses O(N) time 36 Evaluation of Mergesort Advantages: • insensitive to the input order • requires about N log2N comparisons for any array of N elements C(N) = 2C(N/2) + N, C(1) = 0 • is stable (preserves the input order of equal elements) • can be used for sorting linked lists • is suitable for external sorting Disadvantage: • requires (in practice) extra memory proportional to N 37 Quicksort! C.
Recommended publications
  • Lecture 11: Heapsort & Its Analysis
    Lecture 11: Heapsort & Its Analysis Agenda: • Heap recall: – Heap: definition, property – Max-Heapify – Build-Max-Heap • Heapsort algorithm • Running time analysis Reading: • Textbook pages 127 – 138 1 Lecture 11: Heapsort (Binary-)Heap data structure (recall): • An array A[1..n] of n comparable keys either ‘≥’ or ‘≤’ • An implicit binary tree, where – A[2j] is the left child of A[j] – A[2j + 1] is the right child of A[j] j – A[b2c] is the parent of A[j] j • Keys satisfy the max-heap property: A[b2c] ≥ A[j] • There are max-heap and min-heap. We use max-heap. • A[1] is the maximum among the n keys. • Viewing heap as a binary tree, height of the tree is h = blg nc. Call the height of the heap. [— the number of edges on the longest root-to-leaf path] • A heap of height k can hold 2k —— 2k+1 − 1 keys. Why ??? Since lg n − 1 < k ≤ lg n ⇐⇒ n < 2k+1 and 2k ≤ n ⇐⇒ 2k ≤ n < 2k+1 2 Lecture 11: Heapsort Max-Heapify (recall): • It makes an almost-heap into a heap. • Pseudocode: procedure Max-Heapify(A, i) **p 130 **turn almost-heap into a heap **pre-condition: tree rooted at A[i] is almost-heap **post-condition: tree rooted at A[i] is a heap lc ← leftchild(i) rc ← rightchild(i) if lc ≤ heapsize(A) and A[lc] > A[i] then largest ← lc else largest ← i if rc ≤ heapsize(A) and A[rc] > A[largest] then largest ← rc if largest 6= i then exchange A[i] ↔ A[largest] Max-Heapify(A, largest) • WC running time: lg n.
    [Show full text]
  • Secure Multi-Party Sorting and Applications
    Secure Multi-Party Sorting and Applications Kristján Valur Jónsson1, Gunnar Kreitz2, and Misbah Uddin2 1 Reykjavik University 2 KTH—Royal Institute of Technology Abstract. Sorting is among the most fundamental and well-studied problems within computer science and a core step of many algorithms. In this article, we consider the problem of constructing a secure multi-party computing (MPC) protocol for sorting, building on previous results in the field of sorting networks. Apart from the immediate uses for sorting, our protocol can be used as a building-block in more complex algorithms. We present a weighted set intersection algorithm, where each party inputs a set of weighted ele- ments and the output consists of the input elements with their weights summed. As a practical example, we apply our protocols in a network security setting for aggregation of security incident reports from multi- ple reporters, specifically to detect stealthy port scans in a distributed but privacy preserving manner. Both sorting and weighted set inter- section use O`n log2 n´ comparisons in O`log2 n´ rounds with practical constants. Our protocols can be built upon any secret sharing scheme supporting multiplication and addition. We have implemented and evaluated the performance of sorting on the Sharemind secure multi-party computa- tion platform, demonstrating the real-world performance of our proposed protocols. Keywords. Secure multi-party computation; Sorting; Aggregation; Co- operative anomaly detection 1 Introduction Intrusion Detection Systems (IDS) [16] are commonly used to detect anoma- lous and possibly malicious network traffic. Incidence reports and alerts from such systems are generally kept private, although much could be gained by co- operative sharing [30].
    [Show full text]
  • Improving the Performance of Bubble Sort Using a Modified Diminishing Increment Sorting
    Scientific Research and Essay Vol. 4 (8), pp. 740-744, August, 2009 Available online at http://www.academicjournals.org/SRE ISSN 1992-2248 © 2009 Academic Journals Full Length Research Paper Improving the performance of bubble sort using a modified diminishing increment sorting Oyelami Olufemi Moses Department of Computer and Information Sciences, Covenant University, P. M. B. 1023, Ota, Ogun State, Nigeria. E- mail: [email protected] or [email protected]. Tel.: +234-8055344658. Accepted 17 February, 2009 Sorting involves rearranging information into either ascending or descending order. There are many sorting algorithms, among which is Bubble Sort. Bubble Sort is not known to be a very good sorting algorithm because it is beset with redundant comparisons. However, efforts have been made to improve the performance of the algorithm. With Bidirectional Bubble Sort, the average number of comparisons is slightly reduced and Batcher’s Sort similar to Shellsort also performs significantly better than Bidirectional Bubble Sort by carrying out comparisons in a novel way so that no propagation of exchanges is necessary. Bitonic Sort was also presented by Batcher and the strong point of this sorting procedure is that it is very suitable for a hard-wired implementation using a sorting network. This paper presents a meta algorithm called Oyelami’s Sort that combines the technique of Bidirectional Bubble Sort with a modified diminishing increment sorting. The results from the implementation of the algorithm compared with Batcher’s Odd-Even Sort and Batcher’s Bitonic Sort showed that the algorithm performed better than the two in the worst case scenario. The implication is that the algorithm is faster.
    [Show full text]
  • Quick Sort Algorithm Song Qin Dept
    Quick Sort Algorithm Song Qin Dept. of Computer Sciences Florida Institute of Technology Melbourne, FL 32901 ABSTRACT each iteration. Repeat this on the rest of the unsorted region Given an array with n elements, we want to rearrange them in without the first element. ascending order. In this paper, we introduce Quick Sort, a Bubble sort works as follows: keep passing through the list, divide-and-conquer algorithm to sort an N element array. We exchanging adjacent element, if the list is out of order; when no evaluate the O(NlogN) time complexity in best case and O(N2) exchanges are required on some pass, the list is sorted. in worst case theoretically. We also introduce a way to approach the best case. Merge sort [4] has a O(NlogN) time complexity. It divides the 1. INTRODUCTION array into two subarrays each with N/2 items. Conquer each Search engine relies on sorting algorithm very much. When you subarray by sorting it. Unless the array is sufficiently small(one search some key word online, the feedback information is element left), use recursion to do this. Combine the solutions to brought to you sorted by the importance of the web page. the subarrays by merging them into single sorted array. 2 Bubble, Selection and Insertion Sort, they all have an O(N2) time In Bubble sort, Selection sort and Insertion sort, the O(N ) time complexity that limits its usefulness to small number of element complexity limits the performance when N gets very big. no more than a few thousand data points.
    [Show full text]
  • Selected Sorting Algorithms
    Selected Sorting Algorithms CS 165: Project in Algorithms and Data Structures Michael T. Goodrich Some slides are from J. Miller, CSE 373, U. Washington Why Sorting? • Practical application – People by last name – Countries by population – Search engine results by relevance • Fundamental to other algorithms • Different algorithms have different asymptotic and constant-factor trade-offs – No single ‘best’ sort for all scenarios – Knowing one way to sort just isn’t enough • Many to approaches to sorting which can be used for other problems 2 Problem statement There are n comparable elements in an array and we want to rearrange them to be in increasing order Pre: – An array A of data records – A value in each data record – A comparison function • <, =, >, compareTo Post: – For each distinct position i and j of A, if i < j then A[i] ≤ A[j] – A has all the same data it started with 3 Insertion sort • insertion sort: orders a list of values by repetitively inserting a particular value into a sorted subset of the list • more specifically: – consider the first item to be a sorted sublist of length 1 – insert the second item into the sorted sublist, shifting the first item if needed – insert the third item into the sorted sublist, shifting the other items as needed – repeat until all values have been inserted into their proper positions 4 Insertion sort • Simple sorting algorithm. – n-1 passes over the array – At the end of pass i, the elements that occupied A[0]…A[i] originally are still in those spots and in sorted order.
    [Show full text]
  • Binary Search
    UNIT 5B Binary Search 15110 Principles of Computing, 1 Carnegie Mellon University - CORTINA Course Announcements • Sunday’s review sessions at 5‐7pm and 7‐9 pm moved to GHC 4307 • Sample exam available at the SCHEDULE & EXAMS page http://www.cs.cmu.edu/~15110‐f12/schedule.html 15110 Principles of Computing, 2 Carnegie Mellon University - CORTINA 1 This Lecture • A new search technique for arrays called binary search • Application of recursion to binary search • Logarithmic worst‐case complexity 15110 Principles of Computing, 3 Carnegie Mellon University - CORTINA Binary Search • Input: Array A of n unique elements. – The elements are sorted in increasing order. • Result: The index of a specific element called the key or nil if the key is not found. • Algorithm uses two variables lower and upper to indicate the range in the array where the search is being performed. – lower is always one less than the start of the range – upper is always one more than the end of the range 15110 Principles of Computing, 4 Carnegie Mellon University - CORTINA 2 Algorithm 1. Set lower = ‐1. 2. Set upper = the length of the array a 3. Return BinarySearch(list, key, lower, upper). BinSearch(list, key, lower, upper): 1. Return nil if the range is empty. 2. Set mid = the midpoint between lower and upper 3. Return mid if a[mid] is the key you’re looking for. 4. If the key is less than a[mid], return BinarySearch(list,key,lower,mid) Otherwise, return BinarySearch(list,key,mid,upper). 15110 Principles of Computing, 5 Carnegie Mellon University - CORTINA Example
    [Show full text]
  • Advanced Topics in Sorting
    Advanced Topics in Sorting complexity system sorts duplicate keys comparators 1 complexity system sorts duplicate keys comparators 2 Complexity of sorting Computational complexity. Framework to study efficiency of algorithms for solving a particular problem X. Machine model. Focus on fundamental operations. Upper bound. Cost guarantee provided by some algorithm for X. Lower bound. Proven limit on cost guarantee of any algorithm for X. Optimal algorithm. Algorithm with best cost guarantee for X. lower bound ~ upper bound Example: sorting. • Machine model = # comparisons access information only through compares • Upper bound = N lg N from mergesort. • Lower bound ? 3 Decision Tree a < b yes no code between comparisons (e.g., sequence of exchanges) b < c a < c yes no yes no a b c b a c a < c b < c yes no yes no a c b c a b b c a c b a 4 Comparison-based lower bound for sorting Theorem. Any comparison based sorting algorithm must use more than N lg N - 1.44 N comparisons in the worst-case. Pf. Assume input consists of N distinct values a through a . • 1 N • Worst case dictated by tree height h. N ! different orderings. • • (At least) one leaf corresponds to each ordering. Binary tree with N ! leaves cannot have height less than lg (N!) • h lg N! lg (N / e) N Stirling's formula = N lg N - N lg e N lg N - 1.44 N 5 Complexity of sorting Upper bound. Cost guarantee provided by some algorithm for X. Lower bound. Proven limit on cost guarantee of any algorithm for X.
    [Show full text]
  • Sorting Algorithm 1 Sorting Algorithm
    Sorting algorithm 1 Sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: 1. The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); 2. The output is a permutation, or reordering, of the input. Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.[1] Although many consider it a solved problem, useful new sorting algorithms are still being invented (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide and conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds. Classification Sorting algorithms used in computer science are often classified by: • Computational complexity (worst, average and best behaviour) of element comparisons in terms of the size of the list . For typical sorting algorithms good behavior is and bad behavior is .
    [Show full text]
  • COSC 311: ALGORITHMS HW1: SORTING Due Friday, September 22, 12Pm
    COSC 311: ALGORITHMS HW1: SORTING Due Friday, September 22, 12pm In this assignment you will implement several sorting algorithms and compare their relative per- formance. The sorting algorithms you will consider are: 1. Insertion sort 2. Selection sort 3. Heapsort 4. Mergesort 5. Quicksort We will discuss all of these algorithms in class. You should run your experiments on the department servers, remus/romulus (if you would like to write your code on another machine that is fine, but make sure you run the actual tim- ing experiments on remus/romulus). Instructions for how to access the servers can be found on the CS department web page under “Computing Resources.” If you are a Five College stu- dent who has previously taken an Amherst CS course or who enrolled during preregistration last spring, you should have an account already set up (you may need to change your password; go to https://www.amherst.edu/help/passwords). If you don’t already have an account, you can request one at https://sysaccount.amherst.edu/sysaccount/CoursePetition.asp. It will take a day to create the new account, so please do this right away. Please type up your responses to the questions below. I recommend using LATEX, which is a type- setting language that makes it easy to make math look good. If you’re not already familiar with it, I encourage you to practice! Your tasks: 1) Theoretical predictions. Rank the five sorting algorithms in order of how you expect their run- times to compare (fastest to slowest). Your ranking should be based on the asymptotic analysis of the algorithms.
    [Show full text]
  • Faster Shellsort Sequences: a Genetic Algorithm Application
    Faster Shellsort Sequences: A Genetic Algorithm Application Richard Simpson Shashidhar Yachavaram [email protected] [email protected] Department of Computer Science Midwestern State University 3410 Taft, Wichita Falls, TX 76308 Phone: (940) 397-4191 Fax: (940) 397-4442 Abstract complex problems that resist normal research approaches. This paper concerns itself with the application of Genetic The recent popularity of genetic algorithms (GA's) and Algorithms to the problem of finding new, more efficient their application to a wide variety of problems is a result of Shellsort sequences. Using comparison counts as the main their ease of implementation and flexibility. Evolutionary criteria for quality, several new sequences have been techniques are often applied to optimization and related discovered that sort one megabyte files more efficiently than search problems where the fitness of a potential result is those presently known. easily established. Problems of this type are generally very difficult and often NP-Hard, so the ability to find a reasonable This paper provides a brief introduction to genetic solution to these problems within an acceptable time algorithms, a review of research on the Shellsort algorithm, constraint is clearly desirable. and concludes with a description of the GA approach and the associated results. One such problem that has been researched thoroughly is the search for Shellsort sequences. The attributes of this 2. Genetic Algorithms problem make it a prime target for the application of genetic algorithms. Noting this, the authors have designed a GA that John Holland developed the technique of GA's in the efficiently searches for Shellsort sequences that are top 1960's.
    [Show full text]
  • Sorting Algorithms
    Sorting Algorithms Next to storing and retrieving data, sorting of data is one of the more common algorithmic tasks, with many different ways to perform it. Whenever we perform a web search and/or view statistics at some website, the presented data has most likely been sorted in some way. In this lecture and in the following lectures we will examine several different ways of sorting. The following are some reasons for investigating several of the different algorithms (as opposed to one or two, or the \best" algorithm). • There exist very simply understood algorithms which, although for large data sets behave poorly, perform well for small amounts of data, or when the range of the data is sufficiently small. • There exist sorting algorithms which have shown to be more efficient in practice. • There are still yet other algorithms which work better in specific situations; for example, when the data is mostly sorted, or unsorted data needs to be merged into a sorted list (for example, adding names to a phonebook). 1 Counting Sort Counting sort is primarily used on data that is sorted by integer values which fall into a relatively small range (compared to the amount of random access memory available on a computer). Without loss of generality, we can assume the range of integer values is [0 : m], for some m ≥ 0. Now given array a[0 : n − 1] the idea is to define an array of lists l[0 : m], scan a, and, for i = 0; 1; : : : ; n − 1 store element a[i] in list l[v(a[i])], where v is the function that computes an array element's sorting value.
    [Show full text]
  • Sorting Algorithms Properties Insertion Sort Binary Search
    Sorting Algorithms Properties Insertion Sort Binary Search CSE 3318 – Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington 9/14/2021 1 Summary • Properties of sorting algorithms • Sorting algorithms – Insertion sort – Chapter 2 (CLRS) • Indirect sorting - (Sedgewick Ch. 6.8 ‘Index and Pointer Sorting’) • Binary Search – See the notation conventions (e.g. log2N = lg N) • Terminology and notation: – log2N = lg N – Use interchangeably: • Runtime and time complexity • Record and item 2 Sorting 3 Sorting • Sort an array, A, of items (numbers, strings, etc.). • Why sort it? – To use in binary search. – To compute rankings, statistics (min/max, top-10, top-100, median). – Check that there are no duplicates – Set intersection and union are easier to perform between 2 sorted sets – …. • We will study several sorting algorithms, – Pros/cons, behavior . • Insertion sort • If time permits, we will cover selection sort as well. 4 Properties of sorting • Stable: – It does not change the relative order of items whose keys are equal. • Adaptive: – The time complexity will depend on the input • E.g. if the input data is almost sorted, it will run significantly faster than if not sorted. • see later insertion sort vs selection sort. 5 Other aspects of sorting • Time complexity: worst/best/average • Number of data moves: copy/swap the DATA RECORDS – One data move = 1 copy operation of a complete data record – Data moves are NOT updates of variables independent of record size (e.g. loop counter ) • Space complexity: Extra Memory used – Do NOT count the space needed to hold the INPUT data, only extra space (e.g.
    [Show full text]