Free Essay

# Sorting

In:

Submitted By Federica0823
Words 910
Pages 4
1. Use class Sort.java provided in class, the dual-pivot Quicksort of Java 8 (Arrays.sort), and RadixSort.java provided in class.
2. Do the following for Mergesort, Quicksort, Heapsort and dual-pivot Quicksort:
a. Create 100,000 random keys (of type long) and sort them. Repeat this 100 times.
b. Compute the average CPU time taken to sort the keys for the four methods.

c. Comment on the results and compare them to the average-case complexities discussed in class.

As we discussed in the class, all these sorting algorithms I used for testing have the same average-case complexities with O (n log n). But the data I got which shows in the upper diagram illustrate that Quick sort is the most fast sorting method especially when you need to sort a large amount of data.
Dual-pivot quicksort is also fast than merge sort and Heap sort. Heap sort can be used when the pending data between 1000-1000, 000, and for Merge sort is the first choice when there are more than 1M data.
3. Do the following for the four sorting methods of #2, and for Radix sort:
a. Create 100,000 random strings of length 4 and sort them using the five sorting methods.
b. Repeat (a) 10 times Compute the average CPU time taken to sort the keys for the five methods.

c. Repeat (a) and (b) with strings of length 6, 8, 10.

d. Create a table with the results and compare the times with the average-case and worstcase complexities as studied in class.
The length of string is 4:

The length of string is 6:

The length of string is 8:

The length of string is 10:

The upper diagrams and tables show that when the length of string changed, the total using time of five kinds of different sorting methods are diverse. And we can see that when string’s length is 4, Radix sort used the least time to sort 100000 random strings while Heap sort was the lowest. But as the length of string increasing, the property of Radix sort became lower and when the length of string was over 8, it was the slowest sorting algorithm.

As we discussed in the class, the average cases of the other four sorting methods are O (nlogn) except Radix sort. Radix sort can run in O (nlogn) only when the value of d is log (n) and N = O (n). The worst case of this sorting method is O (d (n+N)) and for Quick sort is O (n*n) while Merge sort and Heap sort are stabilizing with the worst-case running time of O (nlogn).
4. Comment on: which sorting method will you use in your applications? in which case? Why? Here we mainly tested on four kinds of sorting methods: Mergesort, Quicksort, Heapsort and dual-pivot Quicksort. As the data analysis did for test result, the time complexity of the Mergesort is O(nlogn) and the space complexity is O(n); the best time complexity of the Quicksort is O(nlogn) and the worst is O(n^2), the space complexity is O(nlogn); the time complexity of the Heapsort is O(nlogn) no matter in best case or the worst case; the time complexity of the dual-pivot Quicksort in best case is O(nlogn) and O(n^2) in the worst case.

Actually there are also many other kinds of sorting methods such as Selectionsort, Insertsort, Bubblesort, Radixsort and ChainRadixsort. As different methods have unequal features, I will select an optimum method according to various circumstances. Personally, when n is small, I will prefer to use insertsort or Selectionsort; when n is large, I will choose Heapsort or Mergesort; when n is large but the keys have few bits, ChainRadixSort is my first choice; when all data are mainly in order, Insertsort or Bubblesort is better to save time, and Quicksort is best for the unordered data.
In a word, Mergesort, Bubblesort, Insertsort and Radixsort are the stabilized sorting methods. Quicksort, Heapsort and Mergesort have the time complexity with O(nlogn), and both Heapsort and Mergesort have the same time complexity with O(n^2) no matter in the best case or the worst case.
5. Create a simple array.
a. Write a method that creates an array of size 10 and insert one million integers by using the incremental strategy.

b. Write a method that does the same as (a), but uses d-tupling; that is, it increases the size of the array by d*N.

c. Test your program of (a) for values of c = 10, 50, 100, 200, 500, 1000, 2000, 5000 and 10000. Do the same for (b) for values of d = 1.2, 1.4, …, 3.8, 4.0. Record the CPU times for each case and draw a table.

d. Comment on the results obtained… which strategy will you use. Which value of d will you choose? Why?
As we can see from the data gained before, two strategies have different properties when the variant change. For me, I prefer to use d-tupling when insert integers into an array with an initial value as this strategy is more fast, more flexible and easy to implement. What is more , as the data I got, when the value of d is 3.2, total using time is the least, so I will initialize d’s value of 3.2.

### Similar Documents

Free Essay

#### Sorting Algorithms

...REVIEW ON SORTING ALGORITHMS A comparative study on two sorting algorithms By Pooja Adhikari A Term Paper Submitted to the Faculty of Dr. Gene Boggess Mississippi State University In the Department of Computer Science & Engineering Mississippi State, Mississippi 04 20072 ABSTRACT Any number of practical applications in computing requires things to be in order. The performance of any computation depends upon the performance of sorting algorithms. Like all complicated problems, there are many solutions that can achieve the same results. One sort algorithm can do sorting of data faster than another. A lot of sorting algorithms has been developed to enhance the performance in terms of computational complexity, memory and other factors. This paper choose two of the sorting algorithms among them selection sort and shell sort and compares the various performance factor among them. 1. INTRODUCTION Sorting is the rearrangement of things in a list into their correct lexicographic order. A number of sorting algorithms have been developed like include heap sort , merge sort, quick sort, selection sort all of which are comparison based sort .There is another class of sorting algorithms which are non comparison based sort. This paper gives the brief introduction about sorting algorithms [2] where it discuss about the class of sorting algorithms and their running times. It mainly analyses the performance between two...

Words: 841 - Pages: 4

Free Essay

#### Reserch Paper on Sorting and Dictionaries

...study on sorting and dictionaries Sujeet Kumar Department of Information and technology Dronacharya College of Engineering, Gurgaon-122001, India Email:sujeet.16939@ggnindia.dronacharya.info, Ashish Gupta Department of Information and technology Dronacharya College of Engineering, Gurgaon-122001, India Email:ashish.16907@ggnindia.dronacharya.info Abstract-Arrays and linked lists are two basic data structures used to store information. We may wish to search, insert or delete records in a database based on a key value. This section examines the performance of these operations on arrays and linked lists. In this research paper we studied about the sorting and types of sorting. This is followed by techniques for implementing dictionaries, structures that allow efficient search, insert, and delete operations. We have also illustrated algorithms that sort data and implement dictionaries for very large files Keywords-insertion sort, quick sort, sort ,binary search tree, skip list. 1. Introduction Arrays and linked lists are two basic data structures used to store information. We may wish to search, insert or delete records in a database based on a key value. This section examines the performance of these operations on arrays and linked lists. 1.1 Arrays Figure 1-1 shows an array, seven elements long, containing numeric values. To search the array sequentially, we may use the algorithm...

Words: 2248 - Pages: 9

Free Essay

#### Experience at Food Bank

...My experience at the Houston Foodbank I have always had a passion for getting involved in community activities and helping people as much as I can, being able to translate that passion into a reality at the Houston Foodbank was a memorable experience for me. The activities of the day started at 8:00 am with registration and a welcome address from the staff at the Foodbank. After the address, the group was split into two subgroups, people with blue tags and those with orange tags. I was part of the group with the blue tags. One of the leaders took us into a sorting room where we had to separate and sort items from big boxes into smaller banana boxes. The items range from cereal to drugs to toys and other household products. Items such as water, soda and items that were too big to go into the banana boxes were placed into different boxes. Once a banana box has been filled, it is covered and carried to the packaging unit for proper packing and storing. We did this for about an hour until one of the leaders pulled the UHD students out of the room as it was getting too crowded. The UHD students were then moved to a separate unit where we had to further group the items packed into banana boxes into different categories which includes non-food products, drugs, condiments and soda, dry foods and snacks. Expired food items were placed into a separate container to avoid giving them out. We worked in this unit until the end of the period. Working at the Houston Foodbank taught me a...

Words: 398 - Pages: 2

#### Instinct

...You got ball, “BREAK A LEG” One glance at the assignment’s title and the first thing came to my mind, “What in the world is this?” Is it literally my lecturer wants me to organize a fist fighting sort of a competition where we challenge guys to fight with each other by kicking their guts out? My thoughts went wild and I start imagining Brad Pitt look alike guys flexing their well define sculpted body with sweat dripping from that crunchy abdominal muscles. The reality was far apart from my imagination. Yes, there will be guys involve. Yes, they will compete with each other and yes, they will even flex their muscles, but for a completely different reason. It’s a futsal competition! I was thrilled with this as it will be my first ever venture in organizing sports event. Moreover, the title of the event itself was so catchy that I was so eager to promote it. You got ball, “Break a Leg” merely means there is a ball in the court, “Good Luck”. That very moment, I knew this event is going to be a challenge as I may end up with too much on my plate to chew. With all the weekends occupied in doing study revisions and weekdays by attending classes, I have to squeeze through whatever little time left to materialise this event. I was being very optimistic while carrying the entire task and this really helped to pull me through the muddy grounds when it was hard to decorate my every single step. We started this assignment by conducting meetings. Meetings were held weekly with our...

Words: 606 - Pages: 3

Free Essay

#### Se This Method If You'D Like to Upload a Document from Your Computer. We Support the Following File

...Proceedings of 2010 IEEE 17th International Conference on Image Processing September 26-29, 2010, Hong Kong K-NEAREST NEIGHBOR SEARCH: FAST GPU-BASED IMPLEMENTATIONS AND APPLICATION TO HIGH-DIMENSIONAL FEATURE MATCHING ´ Vincent Garcia1 , Eric Debreuve2 , Frank Nielsen1,3 , Michel Barlaud2 2 Ecole Polytechnique, Laboratoire d’informatique LIX, 91128 Palaiseau Cedex, France Laboratoire I3S, 2000 route des lucioles, BP 121, 06903 Sophia Antipolis Cedex, France 3 Sony CSL 3-14-13 Higashi Gotanda, Shinagawa-Ku, 141-0022 Tokyo, Japan rithm. Behind its apparent simplicity, this algorithm is highly demanding in terms of computation time. In the last decades, several approaches [4, 5] have been proposed with one common goal: to reduce the computation time. These methods generally seek to reduce the number of distances that have to be computed using, for instance, a pre-arrangement of the data. The direct consequence is a speed-up of the searching process. However, in spite of this improvement, the computation time required by the kNN search still remains the bottleneck of methods based on kNN. General-purpose computing on graphics processing units (GPGPU) is the technique of using a Graphics processing unit (GPU) to perform the computations usually handled by the CPU. The key idea is to use the parallel computing power of the GPU to achieve signiﬁcant speed-ups. Numerous recent publications use the GPU programming to speed-up their methods [6, 7]. In a previous work [8], we...

Words: 3725 - Pages: 15

Free Essay

#### Ffff

...CS201 Final Study Guide I. Basic Linux Commands a. cd – change directory b. mkdir – make directory c. ls – show contents of directory II. Variables in Python d. Variables do not need to be declared e. Must start with a letter or “_” i. can contain numbers f. **variable names are case sensitive! ii. Use camel case: 1. newValue 2. studentAverage 3. setAverageGrade g. Initializing a variable: iii. 3 2 1 iv. variable = value h. Order of Operations: v. HIGHEST PRIORITY: not ** vi. * / % vii. + - viii. < <= > >= ix. == != x. and xi. LOWEST PRIORITY: or III. Python Data Types (int, float, strings, lists, boolean, NoneType) and the type function i. Int – integer numbers: 5, 6, 3, 25, etc. j. Float – decimals: 5.0, 3.50, 25.4, etc. k. String – string of words or a phrase: “Hello World” etc. l. List – a list of values contained within brackets: [1, 4, 3, 25] m. Boolean – True/False ...

Words: 4622 - Pages: 19

Free Essay

#### Blah

...LIST OF PRACTICALS OF C++ (CLASS XII) 1. WRITE MENU DRIVEN PROGRAM TO SHOW FOLLOWING OPERATIONS IN A 1-D ARRAY (USING USER DEFINED FUNCTION) MENU 1. CREATION OF AN ARRAY 2. SEARCHING ARRAY USING - 3. LINEAR SEARCH METHOD. BINARY SEARCH METHOD. SORTING ARRAY USING - SELECTION SORT - BUBBLE SORT - INSERTION SORT - MERGE SORT 4. INSERTING AN ELEMENT AT iTH POSITION 6. DELETING AN ELEMENT FROM AN ARRAY 7. 2. MERGE TWO ARRAYS OF INTEGERS IN ASCENDING OR DESCENDING ORDER 5. QUIT WRITE MENU DRIVEN PROGRAM TO SHOW FOLLOWING OPERATIONS IN A 2-D ARRAY (USING USER DEFINED FUNCTION) MENU 1. ADDING TWO 2-D ARRAYS 2. SUBSTRACTING TWO 2-D ARRAYS 3. MULTIPLYING TWO 2-D ARRAYS 4. CHECK WHETHER TWO 2-D ARRAYS ARE EQUIVALENT OR NOT 5. DISPLAY UPPER TRIANGULAR MATRIX 6. DISPLAY LOWER TRIANGULAR MATRIX 7. DISPLAY AND FIND SUM OF DIAGONAL ELEMENTS OF A 2-D ARRAY 8. DISPLAY AND FIND THE ROW-WISE SUM OF A 2-D ARRAY 9. DISPLAY AND FIND THE COLUMN-WISE SUM OF A 2-D ARRAY 10. QUIT 3. USING STRUCTURES WRITE A MENU DRIVEN PROGRAM TO ADD, SUBTRACT AND MULTIPLY AND DIVIDE TWO COMPLEX NUMBERS 4. USING STRUCTURES WAP TO CHECK THE VALIDY OF DATE 5. WRITE A PROGRAM TO DEFINE THE CLASS WORKER SHOWN BELOW CLASS WORKER ( PRIVATE : WNAME CHARACTER (20), WNO INTEGER, WGRATE FLOAT, HOURLYWAGERATE FLOAT, TOTWAGE FLOAT, CALCWAGE(HRWG,WGRATE) PUBLIC...

Words: 944 - Pages: 4

#### Clump Sort

...Sort For Sorting Medical Data Visvasuresh Victor Govindaswamy Computer Science Texas A&M University-Texarkana Texarkana, USA lovebat814@yahoo.com Matthew Caudill Computer Science Texas A&M University-Texarkana Texarkana, USA Jeff Wilson Computer Science Texas A&M University-Texarkana Texarkana, USA Daniel Brower Computer Science Texas A&M University-Texarkana Texarkana, USA G. Balasekaran, FACSM Medical and Sports Science Nanyang Technology University Singapore Abstract—Sorting data sets are a thoroughly researched field. Several sorting algorithms have been introduced and these include Bubble, Insertion, Selection, Shell, Quick, Merge and Heap. In this paper, we present a novel sorting algorithm, named Clump Sort, to take advantage of ordered segments already present in medical data sets. It succeeds in sorting the medical data considerably better than all the sorts except when using totally non-clumped data. In this test using totally nonclumped data, Heap sort does only slightly better than Clump sort. However, Clump sort has the advantage of being a stable sort as the original order of equal elements is preserved whereas in Heap sort, it is not since it does not guarantee that equal elements will appear in their original order after sorting. As such, Clump Sort will have considerably better data cache performance with both clumped and non-clumped data, outperforming Heap Sort on a modern desktop PC, because it accesses the elements in order. Sorting equal...

Words: 1753 - Pages: 8

#### Computer Science

...websites: |S.No. |Web address |Salient Features | |8. |http://www.java2s.com/Tutorial/C/0260__Data-Structure |A web page on Data Structure| | | |in C | |9. |http://www.java2s.com/Code/C/Data-Structure-Algorithm/CatalogData-Structure-Algorithm.htm |A web page on | | | |Searching/Sorting Techniques| | |...

Words: 1499 - Pages: 6

#### Sort

...Heap Sort For Sorting Medical Data Visvasuresh Victor Govindaswamy Matthew Caudill Jeff Wilson Computer Science Texas A&M University-Texarkana Texarkana, USA lovebat814@yahoo.com Computer Science Texas A&M University-Texarkana Texarkana, USA Computer Science Texas A&M University-Texarkana Texarkana, USA Daniel Brower G. Balasekaran, FACSM Computer Science Texas A&M University-Texarkana Texarkana, USA Medical and Sports Science Nanyang Technology University Singapore Abstract—Sorting data sets are a thoroughly researched field. Several sorting algorithms have been introduced and these include Bubble, Insertion, Selection, Shell, Quick, Merge and Heap. In this paper, we present a novel sorting algorithm, named Clump Sort, to take advantage of ordered segments already present in medical data sets. It succeeds in sorting the medical data considerably better than all the sorts except when using totally non-clumped data. In this test using totally nonclumped data, Heap sort does only slightly better than Clump sort. However, Clump sort has the advantage of being a stable sort as the original order of equal elements is preserved whereas in Heap sort, it is not since it does not guarantee that equal elements will appear in their original order after sorting. As such, Clump Sort will have considerably better data cache performance with both clumped and non-clumped data, outperforming Heap Sort on a modern desktop PC, because it accesses the elements in order. Sorting equal elements...

Words: 1753 - Pages: 8

Free Essay

#### Programming

...TeamLRN Robert Lafore Teach Yourself Data Structures and Algorithms in 24 Hours 201 West 103rd St., Indianapolis, Indiana, 46290 USA Sams Teach Yourself Data Structures and Algorithms in 24 Hours Copyright © 1999 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-31633-1 Library of Congress Catalog Card Number: 98-83221 Printed in the United States of America First Printing: May 1999 01 00 99 4 3 2 1 EXECUTIVE EDITOR Brian Gill DEVELOPMENT EDITOR Jeff Durham MANAGING EDITOR Jodi Jensen PROJECT EDITOR Tonya Simpson COPY EDITOR Mike Henry INDEXER Larry Sweazy PROOFREADERS Mona Brown Jill Mazurczyk TECHNICAL EDITOR Richard Wright Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this...

Words: 10065 - Pages: 41

Free Essay

#### Quick Sort Analysis

...Here is an analysis of the time complexity of quick-sort in detail. In quick sort, we pick an element called the pivot in each step and re-arrange the array in such a way that all elements less than the pivot now appear to the left of the pivot, and all elements larger than the pivot appear on the right side of the pivot. In all subsequent iterations of the sorting algorithm, the position of this pivot will remain unchanged, because it has been put in its correct place. The total time taken to re-arrange the array as just described, is always O(n)1 , or αn where α is some constant [see footnote]. Let us suppose that the pivot we just chose has divided the array into two parts - one of size k and the other of size n − k. Notice that both these parts still need to be sorted. This gives us the following relation: T (n) = T (k) + T (n − k) + αn where T (n) refers to the time taken by the algorithm to sort n elements. WORST CASE ANALYSIS: Now consider the case, when the pivot happened to be the least element of the array, so that we had k = 1 and n − k = n − 1. In such a case, we have: T (n) = T (1) + T (n − 1) + αn Now let us analyse the time complexity of quick sort in such a case in detail by solving the recurrence as follows: T (n) = T (n − 1) + T (1) + αn = [T (n − 2) + T (1) + α(n − 1)] + T (1) + αn (Note: I have written T (n − 1) = T (1) + T (n − 2) + α(n − 1) by just substituting n − 1 instead of n. Note the implicit assumption that the pivot that was chosen divided the original...

Words: 1014 - Pages: 5

#### It 265 Data Structures Phase 5

...Executive Summary 4 Phase 1 4 Phase 2 4 Phase 3 4 Phase 4 4 Phase 5 5 Section 1: Lists, Stacks, and Queues 6 Stacks 6 Queues 10 Section 2: Hashing, Heaps and Trees 14 Section 3: Sorting Algorithms 20 Insertion sort 20 Bubble Sort 20 Selection sort 21 Section 4: Searching 22 Array 22 Linked Lists 23 Section 5: Recursion 30 References 33 Executive Summary Phase 1          A list is a collection of items in which the items have a position (Weiss, 2010).  A linked list allows data to be input or removed easily because each of the data items in the list is connected to its neighbor by a pointer that can be quickly and easily modified to accommodate new or removed items (CTU M.U.S.E. 2014). The Phase 1 portion of this document will be demonstrating the implementation of Stacks and Queues. Phase 2 Hash tables can be viewed as an array of lists and are used to speed up a search for data by creating a situation that does not require the search to start at the beginning and go through every item. The identifying value is the key and the associated record is the data, thus a hash table is a collection of (key, value) pairs. Phase 3 In order to efficiently use a database, the data must be stored in some sort of order. There are a number of different sorting algorithms; a programmer would choose which one to use depending on the amount and type of data being sorted. Insertion sort, Bubble sort, and Selection sort are described with examples. Phase...

Words: 3704 - Pages: 15

Free Essay

#### Doc, Docx, Pdf, Wps, Rtf, Odt

...INDEX s.no | Name of program | Page no. | Remarks | SORTING PROGRAMS | 1 | program of merge sort | | | 2 | program of quick sort | | | 3 | program of bubble sort | | | 4 | program of insertion sort | | | 5 | program of selection sort | | | SEARCHING PROGRAMS | 6 | program of binary search iterative method | | | 7 | Program of binary search by recursive method | | | MULTIPLICATION PROGRAMS | 8 | program to multiply two matrices using direct method | | | 9 | program to multiply two matrices using recursion | | | 10 | program to multiply two matrices using strassen's method | | | //program of merge sort #include<stdio.h> #include<conio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() { int merge[MAX],i,n; clrscr(); printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++) { scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++) { printf("%d ",merge[i]); } getch(); } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high) { int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high))...

Words: 1948 - Pages: 8

Free Essay

#### Thesis

...1. Factorial program in cFactorial program in c: c code to find and print factorial of a number, three methods are given, first one uses for loop, second uses a function to find factorial and third using recursion. Factorial is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Alson! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1. #include <stdio.h>   int main() { int c, n, fact = 1;   printf("Enter a number to calculate it's factorial\n"); scanf("%d", &n);   for (c = 1; c <= n; c++) fact = fact * c;   printf("Factorial of %d = %d\n", n, fact);   return 0; } 2. c program to check odd or evenc program to check odd or even: We will determine whether a number is odd or even by using different methods all are provided with a code in c language. As you have study in mathematics that in decimal number system even numbers are divisible by 2 while odd are not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 ( remainder when four is divided by three). Even numbers are of the form 2*p and odd are of the form (2*p+1) where p is is an integer. #include<stdio.h>   main() { int n;   printf("Enter an integer\n"); scanf("%d",&n);   if ( n%2 == 0 ) printf("Even\n"); else printf("Odd\n");   return 0; } 3. C program to check whether input alphabet is a vowel or notThis code checks whether an input alphabet is a vowel...

Words: 7510 - Pages: 31