Download CmSc250 Design and Analysis of Algorithms

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CmSc250 Introduction to Algorithms
Laboratory exercise 06: Shell sort algorithms
The purpose of this laboratory exercise is to implement Shell sort algorithms and compare their
performance.
.
Task 1:
Download the files ShellSort.java and ShellSortTest.java. ShellSort.java contains the Shell sort
methods, and ShellSortTest.java is a driver to test the Shell sort methods. Insertion sort method,
the original Shell sort method and Knuth sort method are implemented. The ShellSort class
contains stubs for Sedgewick sort and Hibbard sort.
The ShellSort class contains a method
public static int basicSort( int [] array, int [] increments)
that performs the sorting of int [] array with the given increments int [] increments
and returns the number of swaps. This method is a modification of the original Shell sort
method.
The loop
for(gap = size/2…)
is replaced by a loop on the increments in reversed order, and gap takes values from the
corresponding increments[] element.
This method is used by Knuth sort, and it will be used by Hibbard sort and Sedgewick sort
The driver gives the following options:
1) to choose between sorting a small test file and sorting a file with generated numbers
2) If the file is to be generated, further the user can choose between random numbers,
pre-sorted file, and sorted in reverse order file. The size of the file and the upper limit
of the generated numbers is given by the user.
Once the file is specified, the driver runs all the sorts and reports the number of copying
operations (called ‘swaps’)
Before each sorting the array is copied and the copy is sorted.
Each sorting method counts and returns the number of swaps
Task 2: Run the program. Examine how Knuth sort is implemented. In a similar way,
implement Hibbard sort using the sequence of increments proposed by Hibbard:
1, 3, 7, …. 2k -1,… k = 1, 2, …
Task 3: Implement Sedgewick sort using the sequence of increments proposed by Sedgewick:
1, 19, …. 1 + 9*4k - 9*2k,… k = 0, 1, 2, …
1
Note the difference in the start value of k: 1 for Knuth sort and Hibbard sort, 0 for Sedgewick
sort. You’ll have to figure out what changes are necessary to be made in order to account for this
difference.
Task 4: Run the program with various sizes of the array to be sorted and different types of arrays
– random, pre-sorted and sorted in reverse order. Record the number of copying operations in
each case. Find out which sort is appropriate for which case (small arrays / large arrays, sorted /
random / reversed). Record your findings in a written report to be turned in as a Word file.
Task 5: At the end of the Lab send the completed ShellSort.java file.
The complete program and the report are due on Thursday 10/18 by 5 pm.
2