Sorting Networks on FPGA

Total Page:16

File Type:pdf, Size:1020Kb

Sorting Networks on FPGA Recent Researches in Telecommunications, Informatics, Electronics and Signal Processing Sorting networks on FPGA Devi Prasad, Mohamad Yusri Mohamad Yusof, Smruti Santosh Palai, Ahmad Hafez Nawi Microelectronics Department, MIMOS Berhad. Technology Park Malaysia, Kuala Lumpur, Malaysia-57000. [email protected], [email protected], [email protected], [email protected] http://www.mimos.my Abstract: - Speed and efficiency of sorting algorithms are essential for high speed data processing. FPGA based hardware accelerators show better performance than the general purpose processors. Similarly traditional algorithms may not be always efficient on FPGAs. Sorting networks have come as suitable alternatives which can be implemented on FPGAs efficiently. Each application has its own constraint on latency and throughput. A careful selection of a sorting network with suitable number of pipeline stages performs at higher throughput, without contributing much latency. Key-Words: - FPGA, Sorting networks. 1 Introduction and then sorts the columns of the array [10], which With the demand for high speed network and results in the data sequence being partially sorted. As the computing, speed and parallel algorithms have become process repeats the array becomes narrow. Each time the essential tools for development. Many of these number of columns will keep decreasing. In the end, the operations were performed by a general purpose array will have a single column. Shell sort method processor [1]. But now days due to the availability of actually groups the data at each step, rather than sorting FPGAs, many researchers try to implement various the data by itself. At each step, either insertion sort or algorithms on FPGAs more efficiently [2] [3]. FPGAs bubble sort is used to arrange the data. The number of are often used as hardware accelerators. times the data elements need to be rearranged is reduced One of the commonly used operations in high speed data in this type of sorting method. processing is data sorting. The most commonly used sorting algorithm is Bubble sorting. For efficient and reduced operations implementation of sorting, Batcher proposed a technique of sorting using sorting networks [4] [5] [6]. Many of these are implemented of FPGAs and general purpose processors [7] [8] [9]. In this paper we evaluate various sorting networks based on the complexity and speed, focusing on FPGA implementation. For the analysis purposes all the networks are configured to accept eight unsorted numbers, of eight bit wide each. The result will be the sorted numbers. Fig.1. Shell Sort, n=8. 2.3 Odd-even transposition sort 2 Sorting Networks In general, odd-even transposition sort compares the 2.1 Bubble Sort adjacent pair of data in an array to be sorted and, if a In bubble sort the adjacent pair of data elements are pair is found to be in the wrong order then those compared and swapped if they are found in wrong order, elements are swapped. In the first step, odd index and and this process is repeated until the last two elements the adjacent even index elements are compared and are of the array are compared. With each pass in the bubble swapped, if found in wrong order. In the next step, even sort, by compare and swap process the smaller elements index and the adjacent odd index elements are compared bubble or move up to their designated locations in the and are swapped, if found in wrong order. This process array. continues with alternating (odd, even) and (even, odd) 2.2 Shell Sort phases, until no swapping of data elements are required. Shell Sort is one of the oldest sorting algorithms, which Thus the resultant array is a sorted one. This network arranges the data sequence in a two dimensional array comprises of the same number of comparator stages as the number of inputs. In each stage either odd or even ISBN: 978-1-61804-005-3 29 Recent Researches in Telecommunications, Informatics, Electronics and Signal Processing index positions are compared with their respective neighbors. Each stage alternates between even and odd. Fig.4. Odd-even merge sort, n=8. A summary of complexities of above mentioned given in Fig.2. Odd-even transposition Sort, n=8. Table 1. 2.4 Bitonic Sort TABLE 1 Bitonic Sort is a sorting algorithm which is designed for SUMMARY OF COMPLEXITIES parallel machines. On any arbitrary sequence to be Comparator Sort Network Comparators sorted, bitonic sort produces a bitonic sequence by Stages sorting the two halves of the input sequence in opposite Odd-even O(n) O(n2) directions. transposition sort Bubble Sort O(n) O(n2) Bitonic Sort O(log(n)2) O(n·log(n)2) Odd-Even mergesort O(log(n)2) O(n·log(n)2) Shellsort O(log(n)2) O(n·log(n)2) Where n is the number of inputs. 3 FPGA Implementation All the five algorithms are described by Verilog HDL language in two different approaches. One approach is without any pipeline registers and other is with pipeline registers. Each module accepts eight parallel input data of width eight bits each and the clock. The output is the sorted data. The functionality of each of the designed Fig.3. Bitonic Sort, n=8. modules is verified by the simulation in ModelSim , as A bitonic sequence is one, which consists of two sub- shown in Fig.5. sequences, one that monotonically increasing and the other monotonically decreasing. Hence for any arbitrary sequence of length n, in the bitonic sort, first two n/2- element sorts are performed, one increasing and the other is decreasing. This results in an n-element bitonic sequence. This entire sequence is now bitonically sorted to produce a sorted (monotonic) sequence. 2.5 Odd-even merge sort The earlier odd-even transposition sorting algorithm has a complexity of O(n2). With such a complexity, for any large sequence with sequence length n the number of steps to perform a complete sort will be very high in real time situations. Odd- even merge sort solves this problem. In odd-even merge sort, all the odd index elements and even index elements are sorted separately and then merged; this step is repeated until we get a completely sorted sequence. Odd-even merge sort is also called as optimal sorting algorithm. Fig.5. Functional simulation. ISBN: 978-1-61804-005-3 30 Recent Researches in Telecommunications, Informatics, Electronics and Signal Processing Each module is then synthesized and implemented on a similar performance, other sorting algorithms require using Xilinx ISE [11]. Then each module is tested by more number of pipeline stages. downloading the bit file to Xilinx Virtex-5 LX50 FPGA. References: TABLE 2 [1] Amato N., Ravishankar Iyer, Sharad Sundaresan, SUMMARY OF FPGA IMPLEMENTATION Yan Wu, “A Comparison of Parallel Sorting (NO PIPELINE STAGES) Algorithms on Different Architectures,” in Frequency Technical Report No. 98/029, Department of Sort Network LUT FLOP (MHz) Computer Science. Texas A &M University, 1996. Odd-even 1237 128 72 [2] R. Mueller,J. Teubner, G. Alonso, “Data Processing transposition sort on FPGAs,” Proceedings of the VLDB Endowment., vol. 2, Aug. 2009. Bouble Sort 1331 128 46 [3] Martinez J., Cumplido R., Feregrino C., “An FPGA- Bitonic Sort 800 128 85 based parallel sorting architecture for the Burrows Odd-Even 654 128 101 Wheeler transform,” Reconfigurable Computing and mergesort FPGAs., 2005. Shellsort 885 128 61 [4] Y.Jun, Li Na, D. Jun, Guo Y., Tang Z., “A research of high-speed Batcher's odd-even merging network,” TABLE 3 E-Health Networking, Digital Ecosystems and SUMMARY OF FPGA IMPLEMENTATION Technologies (EDT), Vol.1, pp. 77-80, April 2010. (WITH PIPELINE STAGES) [5] K.E. Batcher, “Sorting networks and their applications,” Proceedings of the AFIPS Spring Joint Freque Pipelin Computer Conference 32, pp. 307–314, July 1968. Sort Network LUT FLOP ncy e [6] Knuth, The Art of Computer Programming – Volume (MHz) Stages 3: Sorting and Searching (2nd Edition). Addison- Odd-even 632 512 353 7 Wesley, 1998. transposition [7] M. Matveev, “Implementation of the sorting sort schemes in a programmable logic,” 6th Workshop on Bouble Sort 650 512 363 12 Electronics for LHC Experiments, pp. 476–478, Bitonic Sort 540 448 353 5 Sept. 2000. Odd-Even 425 368 368 5 [8] R.M. Alonso, D. T. Lucio, “Parallel Architecture for mergesort the Solution of Linear Equation Systems Implemented in FPGA,”Proceedings of the 2009 Shellsort 537 448 360 10 Electronics, Robotics and Automotive Mechanics Table 2 & Table 3 summarize performance of the non Conference (cerma 2009), pp. 275–280. pipeline and pipeline stages of designs. [9] R.M. Alonso, D. T. Lucio, “A Programmable Accelerator for next generation wireless It is observed that pipeline stages based algorithms have communications,” 17th European Signal Processing better operating frequency than the non pipelined stage Conference (EUSIPCO 2009), pp. 1294–1298. algorithms. On the contrary, pipeline stages consume [10] D.L. Shell, “A High-Speed Sorting Procedure,” more FPGA resources than the non pipeline stages. The Communications of the ACM., pp. 30–32, 1959. Odd-even merge sort consumes fewer resources and [11] http://www.xilinx.com. gives optimal operating frequency performance compared to the bitonic sort which not only consumes more resources but also has a poor operating frequency performance. Both modules have a throughput of sorting eight data per clock with a latency of five. 4 Conclusion It is often critical do decide the best sorting algorithm for a given application. This is based on the tradeoff between pipeline stages, area and speed. It is observed that by adding five pipeline registers for odd even merge sort, throughput can be increased significantly without much increase in area (FPGA resource). To achieve ISBN: 978-1-61804-005-3 31.
Recommended publications
  • Sort Algorithms 15-110 - Friday 2/28 Learning Objectives
    Sort Algorithms 15-110 - Friday 2/28 Learning Objectives • Recognize how different sorting algorithms implement the same process with different algorithms • Recognize the general algorithm and trace code for three algorithms: selection sort, insertion sort, and merge sort • Compute the Big-O runtimes of selection sort, insertion sort, and merge sort 2 Search Algorithms Benefit from Sorting We use search algorithms a lot in computer science. Just think of how many times a day you use Google, or search for a file on your computer. We've determined that search algorithms work better when the items they search over are sorted. Can we write an algorithm to sort items efficiently? Note: Python already has built-in sorting functions (sorted(lst) is non-destructive, lst.sort() is destructive). This lecture is about a few different algorithmic approaches for sorting. 3 Many Ways of Sorting There are a ton of algorithms that we can use to sort a list. We'll use https://visualgo.net/bn/sorting to visualize some of these algorithms. Today, we'll specifically discuss three different sorting algorithms: selection sort, insertion sort, and merge sort. All three do the same action (sorting), but use different algorithms to accomplish it. 4 Selection Sort 5 Selection Sort Sorts From Smallest to Largest The core idea of selection sort is that you sort from smallest to largest. 1. Start with none of the list sorted 2. Repeat the following steps until the whole list is sorted: a) Search the unsorted part of the list to find the smallest element b) Swap the found element with the first unsorted element c) Increment the size of the 'sorted' part of the list by one Note: for selection sort, swapping the element currently in the front position with the smallest element is faster than sliding all of the numbers down in the list.
    [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]
  • CS 758/858: Algorithms
    CS 758/858: Algorithms ■ COVID Prof. Wheeler Ruml Algorithms TA Sumanta Kashyapi This Class Complexity http://www.cs.unh.edu/~ruml/cs758 4 handouts: course info, schedule, slides, asst 1 2 online handouts: programming tips, formulas 1 physical sign-up sheet/laptop (for grades, piazza) Wheeler Ruml (UNH) Class 1, CS 758 – 1 / 25 COVID ■ COVID Algorithms This Class Complexity ■ check your Wildcat Pass before coming to campus ■ if you have concerns, let me know Wheeler Ruml (UNH) Class 1, CS 758 – 2 / 25 ■ COVID Algorithms ■ Algorithms Today ■ Definition ■ Why? ■ The Word ■ The Founder This Class Complexity Algorithms Wheeler Ruml (UNH) Class 1, CS 758 – 3 / 25 Algorithms Today ■ ■ COVID web: search, caching, crypto Algorithms ■ networking: routing, synchronization, failover ■ Algorithms Today ■ machine learning: data mining, recommendation, prediction ■ Definition ■ Why? ■ bioinformatics: alignment, matching, clustering ■ The Word ■ ■ The Founder hardware: design, simulation, verification ■ This Class business: allocation, planning, scheduling Complexity ■ AI: robotics, games Wheeler Ruml (UNH) Class 1, CS 758 – 4 / 25 Definition ■ COVID Algorithm Algorithms ■ precisely defined ■ Algorithms Today ■ Definition ■ mechanical steps ■ Why? ■ ■ The Word terminates ■ The Founder ■ input and related output This Class Complexity What might we want to know about it? Wheeler Ruml (UNH) Class 1, CS 758 – 5 / 25 Why? ■ ■ COVID Computer scientist 6= programmer Algorithms ◆ ■ Algorithms Today understand program behavior ■ Definition ◆ have confidence in results, performance ■ Why? ■ The Word ◆ know when optimality is abandoned ■ The Founder ◆ solve ‘impossible’ problems This Class ◆ sets you apart (eg, Amazon.com) Complexity ■ CPUs aren’t getting faster ■ Devices are getting smaller ■ Software is the differentiator ■ ‘Software is eating the world’ — Marc Andreessen, 2011 ■ Everything is computation Wheeler Ruml (UNH) Class 1, CS 758 – 6 / 25 The Word: Ab¯u‘Abdall¯ah Muh.ammad ibn M¯us¯aal-Khw¯arizm¯ı ■ COVID 780-850 AD Algorithms Born in Uzbekistan, ■ Algorithms Today worked in Baghdad.
    [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]
  • Overview Parallel Merge Sort
    CME 323: Distributed Algorithms and Optimization, Spring 2015 http://stanford.edu/~rezab/dao. Instructor: Reza Zadeh, Matriod and Stanford. Lecture 4, 4/6/2016. Scribed by Henry Neeb, Christopher Kurrus, Andreas Santucci. Overview Today we will continue covering divide and conquer algorithms. We will generalize divide and conquer algorithms and write down a general recipe for it. What's nice about these algorithms is that they are timeless; regardless of whether Spark or any other distributed platform ends up winning out in the next decade, these algorithms always provide a theoretical foundation for which we can build on. It's well worth our understanding. • Parallel merge sort • General recipe for divide and conquer algorithms • Parallel selection • Parallel quick sort (introduction only) Parallel selection involves scanning an array for the kth largest element in linear time. We then take the core idea used in that algorithm and apply it to quick-sort. Parallel Merge Sort Recall the merge sort from the prior lecture. This algorithm sorts a list recursively by dividing the list into smaller pieces, sorting the smaller pieces during reassembly of the list. The algorithm is as follows: Algorithm 1: MergeSort(A) Input : Array A of length n Output: Sorted A 1 if n is 1 then 2 return A 3 end 4 else n 5 L mergeSort(A[0, ..., 2 )) n 6 R mergeSort(A[ 2 , ..., n]) 7 return Merge(L, R) 8 end 1 Last lecture, we described one way where we can take our traditional merge operation and translate it into a parallelMerge routine with work O(n log n) and depth O(log n).
    [Show full text]
  • Bubble Sort: an Archaeological Algorithmic Analysis
    Bubble Sort: An Archaeological Algorithmic Analysis Owen Astrachan 1 Computer Science Department Duke University [email protected] Abstract 1 Introduction Text books, including books for general audiences, in- What do students remember from their first program- variably mention bubble sort in discussions of elemen- ming courses after one, five, and ten years? Most stu- tary sorting algorithms. We trace the history of bub- dents will take only a few memories of what they have ble sort, its popularity, and its endurance in the face studied. As teachers of these students we should ensure of pedagogical assertions that code and algorithmic ex- that what they remember will serve them well. More amples used in early courses should be of high quality specifically, if students take only a few memories about and adhere to established best practices. This paper is sorting from a first course what do we want these mem- more an historical analysis than a philosophical trea- ories to be? Should the phrase Bubble Sort be the first tise for the exclusion of bubble sort from books and that springs to mind at the end of a course or several courses. However, sentiments for exclusion are sup- years later? There are compelling reasons for excluding 1 ported by Knuth [17], “In short, the bubble sort seems discussion of bubble sort , but many texts continue to to have nothing to recommend it, except a catchy name include discussion of the algorithm after years of warn- and the fact that it leads to some interesting theoreti- ings from scientists and educators.
    [Show full text]
  • Data Structures & Algorithms
    DATA STRUCTURES & ALGORITHMS Tutorial 6 Questions SORTING ALGORITHMS Required Questions Question 1. Many operations can be performed faster on sorted than on unsorted data. For which of the following operations is this the case? a. checking whether one word is an anagram of another word, e.g., plum and lump b. findin the minimum value. c. computing an average of values d. finding the middle value (the median) e. finding the value that appears most frequently in the data Question 2. In which case, the following sorting algorithm is fastest/slowest and what is the complexity in that case? Explain. a. insertion sort b. selection sort c. bubble sort d. quick sort Question 3. Consider the sequence of integers S = {5, 8, 2, 4, 3, 6, 1, 7} For each of the following sorting algorithms, indicate the sequence S after executing each step of the algorithm as it sorts this sequence: a. insertion sort b. selection sort c. heap sort d. bubble sort e. merge sort Question 4. Consider the sequence of integers 1 T = {1, 9, 2, 6, 4, 8, 0, 7} Indicate the sequence T after executing each step of the Cocktail sort algorithm (see Appendix) as it sorts this sequence. Advanced Questions Question 5. A variant of the bubble sorting algorithm is the so-called odd-even transposition sort . Like bubble sort, this algorithm a total of n-1 passes through the array. Each pass consists of two phases: The first phase compares array[i] with array[i+1] and swaps them if necessary for all the odd values of of i.
    [Show full text]
  • 13 Basic Sorting Algorithms
    Concise Notes on Data Structures and Algorithms Basic Sorting Algorithms 13 Basic Sorting Algorithms 13.1 Introduction Sorting is one of the most fundamental and important data processing tasks. Sorting algorithm: An algorithm that rearranges records in lists so that they follow some well-defined ordering relation on values of keys in each record. An internal sorting algorithm works on lists in main memory, while an external sorting algorithm works on lists stored in files. Some sorting algorithms work much better as internal sorts than external sorts, but some work well in both contexts. A sorting algorithm is stable if it preserves the original order of records with equal keys. Many sorting algorithms have been invented; in this chapter we will consider the simplest sorting algorithms. In our discussion in this chapter, all measures of input size are the length of the sorted lists (arrays in the sample code), and the basic operation counted is comparison of list elements (also called keys). 13.2 Bubble Sort One of the oldest sorting algorithms is bubble sort. The idea behind it is to make repeated passes through the list from beginning to end, comparing adjacent elements and swapping any that are out of order. After the first pass, the largest element will have been moved to the end of the list; after the second pass, the second largest will have been moved to the penultimate position; and so forth. The idea is that large values “bubble up” to the top of the list on each pass. A Ruby implementation of bubble sort appears in Figure 1.
    [Show full text]
  • Sorting Partnership Unless You Sign Up! Brian Curless • Homework #5 Will Be Ready After Class, Spring 2008 Due in a Week
    Announcements (5/9/08) • Project 3 is now assigned. CSE 326: Data Structures • Partnerships due by 3pm – We will not assume you are in a Sorting partnership unless you sign up! Brian Curless • Homework #5 will be ready after class, Spring 2008 due in a week. • Reading for this lecture: Chapter 7. 2 Sorting Consistent Ordering • Input – an array A of data records • The comparison function must provide a – a key value in each data record consistent ordering on the set of possible keys – You can compare any two keys and get back an – a comparison function which imposes a indication of a < b, a > b, or a = b (trichotomy) consistent ordering on the keys – The comparison functions must be consistent • Output • If compare(a,b) says a<b, then compare(b,a) must say b>a • If says a=b, then must say b=a – reorganize the elements of A such that compare(a,b) compare(b,a) • If compare(a,b) says a=b, then equals(a,b) and equals(b,a) • For any i and j, if i < j then A[i] ≤ A[j] must say a=b 3 4 Why Sort? Space • How much space does the sorting • Allows binary search of an N-element algorithm require in order to sort the array in O(log N) time collection of items? • Allows O(1) time access to kth largest – Is copying needed? element in the array for any k • In-place sorting algorithms: no copying or • Sorting algorithms are among the most at most O(1) additional temp space.
    [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]
  • Chapter 19 Searching, Sorting and Big —Solutions
    With sobs and tears he sorted out Those of the largest size … —Lewis Carroll Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. —Robert Herrick ’Tis in my memory lock’d, And you yourself shall keep the key of it. —William Shakespeare It is an immutable law in business that words are words, explanations are explanations, promises are promises — but only performance is reality. —Harold S. Green In this Chapter you’ll learn: ■ To search for a given value in an array using linear search and binary search. ■ To sort arrays using the iterative selection and insertion sort algorithms. ■ To sort arrays using the recursive merge sort algorithm. ■ To determine the efficiency of searching and sorting algorithms. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 2 Chapter 19 Searching, Sorting and Big —Solutions Self-Review Exercises 19.1 Fill in the blanks in each of the following statements: a) A selection sort application would take approximately times as long to run on a 128-element array as on a 32-element array. ANS: 16, because an O(n2) algorithm takes 16 times as long to sort four times as much in- formation. b) The efficiency of merge sort is . ANS: O(n log n). 19.2 What key aspect of both the binary search and the merge sort accounts for the logarithmic portion of their respective Big Os? ANS: Both of these algorithms incorporate “halving”—somehow reducing something by half. The binary search eliminates from consideration one-half of the array after each comparison.
    [Show full text]
  • Insertion Sort & Quick Sort
    Department of General and Computational Linguistics Sorting: Insertion Sort & Quick Sort Data Structures and Algorithms for CL III, WS 2019-2020 Corina Dima [email protected] MICHAEL GOODRICH Data Structures & Algorithms in Python ROBERTO TAMASSIA MICHAEL GOLDWASSER 12. Sorting and Selection v Why study sorting algorithms? v Insertion sort (S. 5.5.2) v Quick-sort v Optimizations for quick-sort Sorting: Insertion Sort & Quick Sort | 2 Why Study Sorting Algorithms? • Sorting is among the most important and well studied computing problems • Data is often stored in sorted order, to allow for efficient searching (i.e. with binary search) • Many algorithms rely on sorting as a subroutine • Programming languages have highly optimized, built-in sorting functions – which should be used - Python: sorted(), sort() from the list class - Java: Arrays.sort() Sorting: Insertion Sort & Quick Sort | 3 Why Study Sorting Algorithms? (cont’d) • Understand - what sorting algorithms do - what can be expected in terms of efficiency - how the efficiency of a sorting algorithm can depend on the initial ordering of the data or the type of objects being sorted Sorting: Insertion Sort & Quick Sort | 4 Sorting • Given a collection, rearrange its elements such that they are ordered from smallest to largest – or produce a new copy of the sequence with such an order • We assume that such a consistent order exists • In Python, natural order is typically defined using the < operator, having two properties: - Irreflexive property: " ≮ " - Transitive property:
    [Show full text]