akosiraff

Download selectionSort

Apr 12th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/selectionsort/
  3. Requirements: Compare the efficiency of Selection Sort, Insertion Sort and Quicksort.
  4. Approach: Starting from your second homework assignment, evaluate the efficiency of Selection Sort, Insertion Sort and Quicksort. For doing this, you should evaluate their corresponding implementations in each of the 3 cases (best, worst, and average) and count the number of operations performed (assignments, comparisons, and overall, separately).
  5. Selection Sort code:
  6. // Method: selectionSort
  7. // Author: instructorX
  8. // Date: dd/mm/yyyy
  9. // Purpose: A method that sorts an array using a selection sort
  10. public static void selectionSort(Comparable[] array)
  11. {
  12. int i;
  13. for (i = 0; i < array.length; i++)
  14. {
  15. int minIndex = i;
  16. for (int j = i + 1; j < array.length; j++)
  17. if (array[j].compareTo(array[minIndex]) < 0)
  18. minIndex = j;
  19. swap(array, minIndex, i);
  20. }
  21. }
  22. Insertion Sort code:
  23. // Method: insertionSort
  24. // Author: instructorX
  25. // Date: dd/mm/yyyy
  26. // Purpose: A method that sorts an array using an insertion sort
  27. public static void insertionSort(Comparable[] array)
  28. {
  29. for (int i = 1; i < array.length; i++)
  30. {
  31. Comparable temp = array[i];
  32. int j = i - 1;
  33. while (j >= 0 && temp.compareTo(array[j]) < 0)
  34. {
  35. array[j + 1] = array[j];
  36. j--;
  37. }
  38. array[j +1 ] = temp;
  39. }
  40. }
  41. QuickSort Code:
  42. public static void quickSort(int [] items, int leftIndex, int rightIndex)
  43. { int i, j, temp, pivotValue, partitionSize;
  44. partitionSize = rightIndex - leftIndex + 1;
  45. if(partitionSize <= 1) // base case, one item to be sorted
  46. return;
  47. pivotValue = items[(leftIndex + rightIndex) / 2];
  48. i = leftIndex; // initialize the two partition indices
  49. j = rightIndex;
  50. // look for items in wrong partitions and switch them
  51. do
  52. { while (items[i] < pivotValue) // left item is in correct partition
  53. i++;
  54. while (items[j] > pivotValue) // right item is in correct partition
  55. j–;
  56. if (i <= j) // pointers have not crossed, switch items
  57. { temp = items[i]; items[i] = items[j]; items[j]=temp;
  58. i++; j–;
  59. }
  60. } while (i <= j); // the pointers have not crossed
  61. // reduced problems
  62. quickSort(items, leftIndex, j); // sort left partition,
  63. quickSort(items, i, rightIndex); // sort right partition
  64. }
  65. For counting them, you need to add in the right place specific statements to increment the counters for assignments and/or comparisons, wherever needed (actually you have to add for Quicksort only, in case you received no feedback for review/update/change the counters from the second homework assignment).
  66. Draw charts to show how the running time (estimated in numbers of assignments A(n), comparisons C(n), and overall time T(n)=A(n) + C(n)) growths as the size of the input data n is growing. To draw the charts, vary the input size (n) between 100 and 1000, with an increment of maximum 100. For each size, generate the appropriate input sequence (best, worst, and average) for the specific sorting method (be aware: best/worst cases are not necessary the same for the three algorithms), run it, and store the values (A(n), C(n), T(n)).
  67. For the average case, you have to repeat the measurements m times (m=5 should suffice) and report their average. Moreover, for the average case, make sure you always use the same input sequence for all three sorting methods for a fair comparison.
  68. For each of the analyzed cases, generate charts which compare the three methods; use different charts for the number of comparisons, number of assignments and total number of operations. Name your charts and curves on each chart appropriately (that is, specify what the chart/curve represents).
  69. Feedback on Second Homework Assignment: Your solution fails to display an i/o sequence to show the algorithms are sorting. It was requested “to show your algorithms are sorting (to display the input and output sequences)”
  70. Indexes shouldn’t be counted. Reason: the indexes are integers, and operations on integers are performed much faster than operation on other types of data – and in the real world scenarios, the data is some structured data, with several components, and we first have to reach to the item (the key, which is not necessarily an int) based on which the sorting is performed and do the comparison and the rest of the task.
  71. In selection sort the comparison (array[j].compareTo(array[minIndex]) < 0) is done no matter what’s its result (true or false). Hence, it should have been counted outside the conditional statement (that is in both braches, then and else, not just in one branch).
  72. In insertion sort the comparison (temp.compareTo(array[j]) < 0) is done once more, by the time it is evaluated to false, and the execution exits the while loop.
  73. Why so many arrays? Just 2 of the max size is enough to run all the tests!
  74. Diagrams: please group them by case and by measure (on separate charts) as it is difficult to follow when so many curves are on the same diagram (no penalty on this – this time).
  75. Deliverables: You should submit (1) all the source (.java) files, (2) an output sample (screenshot showing program execution and the results of your testing, to show the methods are actually sorting) (3) the diagrams (the easiest way to draw them is by using Microsoft Excel Worksheet) and (4) a document file describing your solution. The solution description document should include the following elements: justification for the added statements (the ones that increment the counters), the choice for the input data for best and worst cases for each algorithm, with justification, the interpretation of the diagrams and lessons learned.
  76.  
  77. Download: http://solutionzip.com/downloads/selectionsort/
Add Comment
Please, Sign In to add comment