





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Class: Introduction to Algorithms; Subject: Computer Science; University: Illinois Institute of Technology; Term: Spring 2007;
Typology: Study Guides, Projects, Research
1 / 9
This page cannot be seen from the preview
Don't miss anything!






The objective of this project is to implement and compare five different sorting methods on arrays of integers; insertion sort, binary insertion sort, quicksort, heapsort and mergesort.
I implemented with Java as object- oriented language. Each sorting program was implemented each class. You can put data through the file “input.in”. After running the program, you can find the file ‘input.in.out’ represent to result. All data input from inputarray[1] to end of them. You can measure the time to spend on sorting. You can create numbers randomly. For example, if you put ‘r 100 20 30000 ‘. ‘r’ means random and first number means number of integers you want to sort, and second number means minimum numbers, final number represents maximum numbers. So, it will create 100 random numbers between 20 and 30000. there are two quicksort methods. (quickSort1 – set the first element as the pivot value. quickSort2 – set the pivot value to random)
Each value is the average value of multiple running the sort at 100 times. Table 1 is the result of the running times of each algorithm.
Following the results, you can see that the running times of insertion sort and binary insertion sort are very similar. And other sorts (heap sort, merge sort, quick sort) are similar in the running times. You can understand more clearly with figure1 and figure 2.
You can also see that the linear graphs of the running time of insertion sort and binary insertion sort are very similar to the graph of f(n)=n² when n=1000,2000,4000,8000,16000. Furthermore, you can see that the linear graphs of quicksort, heapsort and merge sort are also similar to that of g(n)=n lg n. Therefore we can assume that the running times of insertion sort and binary insertion sort are O(n²) and that of quicksort, heapsort and mergesort are O(n lg n). We can see more clearly with following data analysis.
1. Insertion Sort
1000 2. 2000 8. 4000 34. 8000 136. 16000 597.
The values of n² are proportional to the running times. Therefore, we can say that the running time of insertion sort in practice follows O(n²) that of worst-case and average case by the analysis in the text.
Binary insertion
Merge 0.47 0.62 1.72 2.5 3.13 4.22 4.86 5.94 7. Heap 0.46 0.63 1.41 2.03 2.66 3.75 3.9 5 6. Quick1 0.16 0.46 1.09 1.41 1.87 2.5 2.96 3.44 4. Quick2 0.31 0.47 0.94 1.41 1.73 2.5 2.81 3.75 4.
Comparisons of Sorting algorithm
0
100
200
300
400
500
600
700
1000 2000 4000 6000 8000 10000 12000 14000 16000
milliseconds
Insertion Binary insertion Merge Heap Quick1 Quick
Insertion 2.19 8.75 33.29 135.63 559. Binary insertion 2.19 8.59 31.75 133.53 544. Merge 0.32 0.63 1.56 3.29 7. Heap 0.46 0.63 1.25 2.65 6. Quick1 0.15 0.46 0.94 1.87 4. Quick2 0.16 0.32 0.78 1.88 4.
comparison of sorting algorithm
0
100
200
300
400
500
600
1000 2000 4000 8000 16000
Milliseconds
Insertion Binary insertion Merge Heap Quick1 Quick
Merge 0.47 0.62 1.72 2.5 3.13 4.22 4.86 5.94 7. Heap 0.46 0.63 1.41 2.03 2.66 3.75 3.9 5 6. Quick1 0.16 0.46 1.09 1.41 1.87 2.5 2.96 3.44 4. Quick2 0.31 0.47 0.94 1.41 1.73 2.5 2.81 3.75 4.
comparison of sorting algorithm
0
2
4
6
8
1000 2000 4000 6000 8000 10000 12000 14000 16000
m
illise
co
nds
Merge Heap Quick1 Quick
In the best case of an already sorted array, insertion sort takes O(n) time: in each iteration, the first remaining element of the input is only compared with the last element of
subarrays of n/2 elements each are merged. So merge sort requires additional memory.