Question
OBJECT ORIENTED PROGRAMMING JAVA PROGRAMMING This is an example for this assignment : package sorting; import java.util.*; import java.math.*; public class Sorting { static int
OBJECT ORIENTED PROGRAMMING
JAVA PROGRAMMING
This is an example for this assignment :
package sorting;
import java.util.*; import java.math.*;
public class Sorting { static int QuickCount = 0; static int BubbleCount = 0; // print an array of integers, 10 elements per line public static void print (int arr []) { int on_line; // number of elements printed on line on_line = 0; // Print each element in turn for (int element : arr) { // need to go to a new line if (on_line == 10) { System.out.println (); on_line = 0; } System.out.print (element + " "); on_line++; } } // swap elements at indexes i1 and i2 public static void swap (int arr [], int i1, int i2) { int swapper; swapper = arr [i1]; arr [i1] = arr [i2]; arr [i2] = swapper; } public static void BubbleSort (int arr []) { boolean swapped; // swapped is true, if values were swapped int index; // current spot in the array swapped = true; // continue doing passes of the array until all // elements are sorted while (swapped) { swapped = false; // do a pass of the algorithm for (index = 0; index arr [index + 1]) { swap (arr, index, index + 1); swapped = true; } } } } // Quicksort that just generates low and high indexes public static void QuickSort (int arr []) { QuickSortHelp (arr, 0, arr.length - 1); } // recursive QuickSort, low is starting index into the // array section to be sorted, high is the ending index // into the array to be sorted public static void QuickSortHelp (int arr [], int low, int high) { int pivot; // value separating sets (first in array) int lowMover, // index into first part of array highMover; // index into last part of array // if one element, already sorted, so only concerned // with case of more than one element if (low = low && arr [highMover] > pivot) { QuickCount++; highMover--; } // repeat the above process as long as there are // values to swap while (lowMover = low && arr [highMover] > pivot) { QuickCount++; highMover--; } } // move pivot to the middle swap (arr, low, highMover); // call recursively to sort the two "halves" QuickSortHelp (arr, low, highMover - 1); QuickSortHelp (arr, highMover + 1, high); } } /** * @param args the command line arguments */ public static void main(String[] args) { int [] arr = new int [1000]; // array to be sorted // initialize the array with random integers for (int i = 0; i
System.out.println (" The sorted array after Bubble Sort is:"); print (arr); // initialize the array with random integers System.out.println (" "); for (int i = 0; i
System.out.println (" The sorted array after QuickSort is:"); print (arr); System.out.println (" Bubble comparisons: " + BubbleCount); System.out.println ("QuickSort comparisons: " + QuickCount); System.out.println (" "); } }
Output of the Program
run: The original array is: 2564 5737 9192 6586 5510 9758 7302 9758 1949 5920 2687 4306 8804 4506 4545 8915 3186 2868 8276 760 9260 8251 3343 9321 3941 680 9680 4309 7697 2236 3713 9474 6679 1004 2032 4322 8943 8926 9342 9992 631 2882 7431 3892 7630 7387 3599 99 4438 5514 217 8955 477
2349 8914 6539 4984 9985 3569 7939 1886 6141 6636 3522 9657 7877 1174 4043 8634 9462 3906 8646 2813 1387 4641 9509 7407 616 7431 1088 136 3180 7328 5533 4392 2774 7406 8000 4015 3282 5629 4287 97 8836 9953 450 7403 197 4006 6851 5186 6932 1022 1039 4544 9557 447 388 3235 9982 412 7404 1699 7378 7144 4604 5189 3557 664 6824 3637 297 3965 5743 6219 3836 2860 3307 4639 1986 175 1760 7653 3561 1110 8017 9550 8164 6206 9215 6929 6754 4607 4491 1426 9889 3076 5220 802 1028 5566 2289 7228 6896 4060 4323 9509 5512 3877 5204 5506 5232 5647 2712 2083 4888 1777 2675 2517 5952 7384 4610 5516 2556
The sorted array after QuickSort is: 0 6 25 53 75 78 80 97 129 129 136 136 143 148 149 150 151 153 170 175 182 190 190 192 194 197 201 205 205 213 219 224 256 297 310 325 350 368 373 384 388 393 412 441 443 447 448 450 455 460 462 465 469 480 488 515 524 533 554 555 568 569 572 575 576 579 583 583 584 596 598 600 603 616 664 669 688 705 716 722 738 769 797 797 802 809 819 820 822 829 854 861 862 911 915 916 922 925 938 958 969 978 997 998 1020 1021 1022 1028 1031 1039 1060 1067 1080 1088 1095 1103 1110 1120 1122 1124 1135 1139 1147 1155 1164 1168 1170 1172 1174 1180 1191 1195 1211 1214 1225 1234 1255 1259 1267 1283 1327 1332 1341 1370 1387 1426 1435 1437 1441 1489 1491 1494 1500 15 6770 6774 679 9954 9955 9962 9970 9975 9980 9982 9985 9988 9998 Bubble comparisons: 990009 QuickSort comparisons: 12246
.o-E- 11NormalltNoSpae.. Heading 1 Heading 2 .2- . Title Paragraph Styles Java Programming- A Sorted Affair 1. Add the Gnome Sort to the Bubble Sort and QuickSort program completed in class. See: https://en.wikipedia.org/wiki/Gnome sort Count and display the number of comparisons needed for each of the sorts. 2. Run the sorts with data set sizes of 100, 200, 300, 400, and 500. 3. Create an Excel data sheet displaying the above values of n and for each n the following results: a. Comparisons for the Gnome Sort b. Comparisons for the Bubble Sort EeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started