Sorting Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
Load more
Recommended publications
-
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. -
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. -
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. -
Fall, 2016 Lab #3
Texas Christian University CoSc 20803 - Fall, 2016 Lab #3 Points: 100 points Language: Java Due Date: Midnight, Thursday, Nov 17, 2016 (complete Java project and manually-generated Excel spreadsheet ZIPped together and submitted with TURNIN). Purpose: The purpose of this assignment is to allow you to evaluate the performance of several different sorting algorithms on data in various degrees of disorder. Processing: You should implement the following sorting algorithms and gather statistics as noted below: Method #1 Method #2 Method #3 Method #4 Bubblesort Cocktail Shaker Sort Shell Sort Quick Sort (pp. 230-232) (discussed in class) (discussed in class) (pp. 232-236) Input: Three input data sets will be provided (each of which contains 5000, 4-byte alphanumeric keys). The files are: Ascending.dat, Descending.dat, and Random.dat (obtainable from the CoSc Department’s class website: GUI: For this lab you should develop a manual data collection program that employs a VERY SIMPLE graphical user interface. Your program should accommodate input from the user specifying: a) the integer number of records to be sorted, b) the name of the input file to be used as the source of data (via a JFileChooser()), and c) the sorting method to use. Your program should allow the sorting methods listed above to be performed. Statistics: You should sort, ascendingly, alphanumeric keys in each of the three data files provided, keeping statistics for each file and for each sort method. Keep track of: (i) the number of key compares (Ki:Kj) and (ii) the number of key moves (e.g., Ki <--- Kj). -
PART III Basic Data Types
PART III Basic Data Types 186 Chapter 7 Using Numeric Types Two kinds of number representations, integer and floating point, are supported by C. The various integer types in C provide exact representations of the mathematical concept of “integer” but can represent values in only a limited range. The floating-point types in C are used to represent the mathematical type “real.” They can represent real numbers over a very large range of magnitudes, but each number generally is an approximation, using a limited number of decimal places of precision. In this chapter, we define and explain the integer and floating-point data types built into C and show how to write their literal forms and I/O formats. We discuss the range of values that can be stored in each type, how to perform reliable arithmetic computations with these values, what happens when a number is converted (or cast) from one type to another, and how to choose the proper data type for a problem. We would like to think of numbers as integer values, not as patterns of bits in memory. This is possible most of the time when working with C because the language lets us name the numbers and compute with them symbolically. Details such as the length (in bytes) of the number and the arrangement of bits in those bytes can be ignored most of the time. However, inside the computer, the numbers are just bit patterns. This becomes evident when conditions such as integer overflow occur and a “correct” formula produces a wrong and meaningless answer. -
Applied C and C++ Programming
Applied C and C++ Programming Alice E. Fischer David W. Eggert University of New Haven Michael J. Fischer Yale University August 2018 Copyright c 2018 by Alice E. Fischer, David W. Eggert, and Michael J. Fischer All rights reserved. This manuscript may be used freely by teachers and students in classes at the University of New Haven and at Yale University. Otherwise, no part of it may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the authors. 192 Part III Basic Data Types 193 Chapter 7 Using Numeric Types Two kinds of number representations, integer and floating point, are supported by C. The various integer types in C provide exact representations of the mathematical concept of \integer" but can represent values in only a limited range. The floating-point types in C are used to represent the mathematical type \real." They can represent real numbers over a very large range of magnitudes, but each number generally is an approximation, using a limited number of decimal places of precision. In this chapter, we define and explain the integer and floating-point data types built into C and show how to write their literal forms and I/O formats. We discuss the range of values that can be stored in each type, how to perform reliable arithmetic computations with these values, what happens when a number is converted (or cast) from one type to another, and how to choose the proper data type for a problem. -
Semi-Automatic Generation of Three-Dimensional Visual Algorithm Simulations
Semi-Automatic Generation of Three-Dimensional Visual Algorithm Simulations Dissertation zur Erlangung des Doktorgrades der Naturwissenschaften Vorgelegt beim Fachbereich Informatik und Mathematik (12) der Goethe-Universitat¨ Frankfurt am Main Von Ashraf Abu Baker April, 2009 Dedication This dissertation is gratefully dedicated to my late loving mother: Nijme Fuad Aref Hmaidy (28.06.1990) iii iv Acknowledgements lthough this dissertation is the result of the author's research, without the A encouragement and support of some people the completion of this thesis would have been much more difficult, if not impossible. To all those people I would like to express my deepest gratitude, especially to my advisers: Prof. Dr. Detlef Kr¨omker, who granted me the opportunity to launch this research, as well as Prof. Dr. Georg Schnitger for their patience, support and guidance. I also owe my sincere gratitude to all those with whom I have published a number of useful papers, for their great cooperation: Borislav Milanovic, Dr. Alexander Tillmann, Dipl.-Inf. Stefan Kappes and Dipl.-Inf. Dirk Grunwald. v vi Publications ome parts of this research have already been published in several papers. In Sparticular, Section 4.3 draws on the work presented in [13] and [12]. Sec- tion 5.5 and 5.7 are based on the research published in [11] and [14], respectively. Two further papers on the visualisation of parallel algorithms and the development of reversible 3D applications were submitted recently to the InfoVis-2009 confer- ence [1] and will not have been reviewed before submitting this thesis. Additionally, four papers were published on topics, not directly related to this research, but to e-learning themes, and are therefore not included in this thesis [17, 16, 15, 121]. -
Data Structures
KARNATAKA STATE OPEN UNIVERSITY DATA STRUCTURES BCA I SEMESTER SUBJECT CODE: BCA 04 SUBJECT TITLE: DATA STRUCTURES 1 STRUCTURE OF THE COURSE CONTENT BLOCK 1 PROBLEM SOLVING Unit 1: Top-down Design – Implementation Unit 2: Verification – Efficiency Unit 3: Analysis Unit 4: Sample Algorithm BLOCK 2 LISTS, STACKS AND QUEUES Unit 1: Abstract Data Type (ADT) Unit 2: List ADT Unit 3: Stack ADT Unit 4: Queue ADT BLOCK 3 TREES Unit 1: Binary Trees Unit 2: AVL Trees Unit 3: Tree Traversals and Hashing Unit 4: Simple implementations of Tree BLOCK 4 SORTING Unit 1: Insertion Sort Unit 2: Shell sort and Heap sort Unit 3: Merge sort and Quick sort Unit 4: External Sort BLOCK 5 GRAPHS Unit 1: Topological Sort Unit 2: Path Algorithms Unit 3: Prim‘s Algorithm Unit 4: Undirected Graphs – Bi-connectivity Books: 1. R. G. Dromey, ―How to Solve it by Computer‖ (Chaps 1-2), Prentice-Hall of India, 2002. 2. M. A. Weiss, ―Data Structures and Algorithm Analysis in C‖, 2nd ed, Pearson Education Asia, 2002. 3. Y. Langsam, M. J. Augenstein and A. M. Tenenbaum, ―Data Structures using C‖, Pearson Education Asia, 2004 4. Richard F. Gilberg, Behrouz A. Forouzan, ―Data Structures – A Pseudocode Approach with C‖, Thomson Brooks / COLE, 1998. 5. Aho, J. E. Hopcroft and J. D. Ullman, ―Data Structures and Algorithms‖, Pearson education Asia, 1983. 2 BLOCK I BLOCK I PROBLEM SOLVING Computer problem-solving can be summed up in one word – it is demanding! It is an intricate process requiring much thought, careful planning, logical precision, persistence, and attention to detail. -
An Enhancement of Major Sorting Algorithms
The International Arab Journal of Information Technology, Vol. 7, No. 1, January 2010 55 An Enhancement of Major Sorting Algorithms Jehad Alnihoud and Rami Mansi Department of Computer Science, Al al-Bayt University, Jordan Abstract: One of the fundamental issues in computer science is ordering a list of items. Although there is a huge number of sorting algorithms, sorting problem has attracted a great deal of research; because efficient sorting is important to optimize the use of other algorithms. This paper presents two new sorting algorithms, enhanced selection sort and enhanced bubble Sort algorithms. Enhanced selection sort is an enhancement on selection sort by making it slightly faster and stable sorting algorithm. Enhanced bubble sort is an enhancement on both bubble sort and selection sort algorithms with O(nlgn) complexity instead of O(n 2) for bubble sort and selection sort algorithms. The two new algorithms are analyzed, implemented, tested, and compared and the results were promising . Keywords: Enhanced selection sort, enhanced bubble sort, selection sort, bubble sort, number of swaps, time complexity . Received May 27, 2008; accepted September 1, 2008 1. Introduction the complexity of solving it efficiently. Bubble sort was analyzed as early as 1956 [2]. Information growth rapidly in our world and to search Many researchers considered sorting as a solved for this information, it should be ordered in some problem. Even so, useful new sorting algorithms are sensible order. Many years ago, it was estimated that still being invented, for example, library sort was first more than half the time on many commercial computers published in 2004. -
An Empirical Study and Analysis on Sorting Algorithms
International Journal of Mechanical Engineering and Technology (IJMET) Volume 8, Issue 8, August 2017, pp. 488–498, Article ID: IJMET_08_08_055 Available online at http://iaeme.com/Home/issue/IJMET?Volume=8&Issue=8 ISSN Print: 0976-6340 and ISSN Online: 0976-6359 © IAEME Publication Scopus Indexed AN EMPIRICAL STUDY AND ANALYSIS ON SORTING ALGORITHMS R Gangadhar Reddy Assistant Professor, Dept. of ECE, Institute of Aeronautical Engineering, Hyderabad, india P R Anisha, C Kishor Kumar Reddy Assistant Professor, Department of CSE, Stanley College of Engineering. & Tech. for Women, Hyderabad M. Srinivasa Reddy, Associate Prof, Dept. of ECE, MLR Institute of Technology, Hyderabad, India ABSTRACT Sorting is an important data structure operation, which makes easy searching, arranging and locating the information. I have discussed about various sorting algorithms with their comparison to each other. I have also tried to show this why we have required another sorting algorithm, every sorting algorithm have some advantage and some disadvantage. Sorting involves rearranging information into either ascending or descending order. Sorting is considered as a fundamental operation in computer science as it is used as an intermediate step in many operations. The goal of this paper is to review on various different sorting algorithms and compares the various performance factors among them. Keywords: Bubble Sort; Selection Sort; Insertion Sort; Quick Sort; Merge Sort; Heap Sort; Cocktail Sort. Cite this Article: R Gangadhar Reddy, P R Anisha, C Kishor Kumar Reddy and M. Srinivasa Reddy, An Empirical Study And Analysis On Sorting Algorithms, International Journal of Mechanical Engineering and Technology 8(8), 2017, pp. 488–498. -
Analysis of Sorting Algorithms Terry D
Southern Illinois University Carbondale OpenSIUC Honors Theses University Honors Program 5-1988 Analysis of Sorting Algorithms Terry D. Fryar Follow this and additional works at: http://opensiuc.lib.siu.edu/uhp_theses Recommended Citation Fryar, Terry D., "Analysis of Sorting Algorithms" (1988). Honors Theses. Paper 230. This Dissertation/Thesis is brought to you for free and open access by the University Honors Program at OpenSIUC. It has been accepted for inclusion in Honors Theses by an authorized administrator of OpenSIUC. For more information, please contact [email protected]. • CHAPTER 1 - INTRODUCTION The concept of order is very important to mankind. Order allows man to understand and better live 1n the world around him. Imagine trying to find a name in a phone book if the names were not in alphabetical order. Computer science is one area where order is especially important. Much of computer science depends on the concept of order. Many commercial and non-commercial programming applications involve sorting items into ascending or descending order. It is precisely the algorithms that perform the task of sorting that we will concentrate on in this report. All of the algorithms sort items in ascending order. There are nine algorithms covered. Each algorithm was chosen based on • simplicity of design, speed, behavior, and interesting properties brought out by analysis. The research concerning these algorithms was broken up into four areas. The first area was the learning of the programming language C. C is a very good language for this type of research because it offers high-level programming structure with low-level features. -
CSC 344 – Algorithms and Complexity What Is the Brute Force Approach?
CSC 344 – Algorithms and Complexity Lecture #3 – Internal Sorting What is the Brute Force Approach? • A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved • Examples: 1. Computing an (a > 0, n a nonnegative integer) 2. Computing n! 3. Multiplying two matrices 4. Searching for a key of a given value in a list Brute-Force Sorting Algorithm • Selection Sort – Scan the array to find its smallest element and swap it with the first element. – Starting with the second element, scan the elements after it to find the smallest among them and swap it with the second elements. – Generally, on pass i (0 ≤ i ≤ n-2), find the smallest element after A[i] and swap it with A[i]: A[0] ≤ . ≤ A[i-1] | A[i], . , A[min ], . ., A[n-1] in their final positions • Example: 7 3 2 5 Selection Sort // selectionSort() - The selection sort where we // seek the ith smallest value and // swap it into its proper place void selectionSort(int x[], int n) { int i, j, min; for (i = 0; i < n-1; i++) { min = i; for (j = i; j < n; j++) { if (x[j] < x[min]) min = j; swap(x[i], x[min]); } } } Analysis of Selection Sort • Time efficiency: • Space efficiency: • Stability: Decrease and Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance to obtain solution to original instance • Can be implemented either top-down or bottom-up • Also referred to as inductive or incremental approach 3 Types of Decrease and Conquer • Decrease by a constant