Question
need help!! java eclipse

Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) alg
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Arrays;

import java.util.Queue;

/**

* This class looks like it's meant to provide a few public static methods

* for searching and sorting arrays. It also has a main method that tests

* the searching and sorting methods.

*

* TODO: The search and sort methods in this class contain bugs that can

* cause incorrect output or infinite loops. Use the Eclipse debugger to

* find the bugs and fix them

*/

public class Sort {

private static int[] numbers;

private static int[] helper;

private static int number;

public void sort(int[] values) {

this.numbers = values;

number = values.length;

this.helper = new int[number];

System.out.println("START");

mergesort(0, number - 1);

System.out.println("END");

}

static void mergeSort(int[] values) {

numbers = values;

number = values.length;

helper = new int[number];

mergesort(0, number - 1);

}

private static void mergesort(int low, int high) {

// Check if low is smaller then high, if not then the array is sorted

if (low < high) {

// Get the index of the element which is in the middle

int middle = (low + high) / 2;

// Sort the left side of the array

mergesort(low, middle);

// Sort the right side of the array

mergesort(middle + 1, high);

// Combine them both

merge(low, middle, high);

}

}

private static void merge(int low, int middle, int high) {

// printParts(low, middle);

//printParts(middle + 1, high);

// Copy both parts into the helper array

for (int i = low; i <= high; i++) {

helper[i] = numbers[i];

}

int i = low;

int j = middle + 1;

int k = low;

// Copy the smallest values from either the left or the right side back

// to the original array

while (i <= middle && j <= high) {

if (helper[i] <= helper[j]) {

numbers[k] = helper[i];

i++;

} else {

numbers[k] = helper[j];

j++;

}

k++;

}

// Copy the rest of the left side of the array into the target array

while (i <= middle) {

numbers[k] = helper[i];

k++;

i++;

}

}

private static int partition(int num[], int p, int q)

{

int piv = num[q];

int i = (p-1);

for (int j=p; j<q; j++)

{

  

if (num[j] <= piv)

{

i++;

int temp = num[i];

num[i] = num[j];

num[j] = temp;

}

}

int temp = num[i+1];

num[i+1] = num[q];

num[q] = temp;

return i+1;

}

private static void qsort(int arr[], int l, int h)

{

if (l < h)

{

int pi = partition(arr, l, h);

qsort(arr, l, pi-1);

qsort(arr, pi+1, h);

}

}

static void quickSort(int val[]){

qsort(val, 0, val.length-1);

}

public static void insertionSort2(int arr[] , int left, int right) {

int in, out;

// sorted on left of out

for (out = left + 1; out <= right; out++) {

int temp = arr[out];

in = out;

// until one is smaller,

while (in > left && arr[in - 1] >= temp) {

arr[in] = arr[in - 1]; // shift item to right

--in;

}

arr[in] = temp;

}

}

private static void qsort2(int arr[], int l, int h)

{

if (l < h)

{

int size = h - l + 1;

if(size<=16){

insertionSort2(arr,l,h);

return;

}

else{

int pi = partition(arr, l, h);

qsort2(arr, l, pi-1);

qsort2(arr, pi+1, h);

}

}

}

static void quickSort2(int val[]){

qsort2(val, 0, val.length-1);

}

public static void main(String[] args) {

for(int n = 100; n<= 10000 ; n = n*10) {

   System.out.println("=====================Array of "+ n +" elements: ====================");

int[] A = new int[n+1]; // Create an array and fill it with small random ints.

for (int i = 0; i < n+1; i++)

   A[i] = 1 + (int)(n * Math.random());

int[] B = A.clone(); // Make copies of the array.

  

long a = System.nanoTime();

Arrays.sort(B);

long b = System.nanoTime();

long Taken = b - a;

System.out.print("\nSorted by Arrays Sort: ");

System.out.println("\nTime taken by Arrays Sort: " + Taken + " ns");

long time1 = System.nanoTime();

bubbleSort(B);

long time2 = System.nanoTime();

long timeTaken = time2 - time1;

  

  

System.out.print("\nSorted by Bubble Sort: ");

System.out.println("\nTime taken by Bubble Sort: " + timeTaken + " ns");

//printArray(B);

  

time1 = System.nanoTime();

selectionSort(B);

time2 = System.nanoTime();

timeTaken = time2 - time1;

System.out.print("\nSorted by Selection Sort: ");

System.out.println("\nTime taken by Selection Sort: " + timeTaken + " ns");

//printArray(B);

  

//

time1 = System.nanoTime();

insertionSort(B);

time2 = System.nanoTime();

timeTaken = time2 - time1;

System.out.print("\nSorted by Insertion Sort: ");

System.out.println("\nTime taken by Insertion Sort: " + timeTaken + " ns");

//printArray(B);

  

time1 = System.nanoTime();

mergeSort(B);

time2 = System.nanoTime();

timeTaken = time2 - time1;

System.out.print("\nSorted by Merge Sort: ");

System.out.println("\nTime taken by Merge Sort: " + timeTaken + " ns");

   // printArray(B);

//

//

time1 = System.nanoTime();

quickSort(B);

time2 = System.nanoTime();

timeTaken = time2 - time1;

System.out.print("\nSorted by Quick Sort: ");

System.out.println("\nTime taken by Quick Sort: " + timeTaken + " ns");

  

}

   // printArray(B);

  

  

// time1 = System.nanoTime();

// quickSort2(B);

// time2 = System.nanoTime();

// timeTaken = time2 - time1;

//

// System.out.print("\nSorted by Quick Sort2: ");

// System.out.println("\nTime taken by Quick Sort2: " + timeTaken + " ns");

//

// printArray(B);

   }

  

   /**

* Tests whether an array of ints contains a given value.

* @param array a non-null array that is to be searched

* @param val the value for which the method will search

* @return true if val is one of the items in the array, false if not

*/

   public static boolean contains(int[] array, int val) {

for (int i = 0; i < array.length;++i) {

   if (array[i] == val)

return true;

}

return false;

   }

  

   /**

* Sorts an array into non-decreasing order. This inefficient sorting

* method simply sweeps through the array, exchanging neighboring elements

* that are out of order. The number of times that it does this is equal

* to the length of the array.

*/

   public static void bubbleSort(int[] array) {

for (int i = 0; i < array.length; i++) {

   for (int j = 0; j < array.length-1; j++) {

if (array[j] > array[j+1]) { // swap elements j and j+1

   int temp = array[j];

   array[j] = array[j+1];

   array[j+1] = temp;

}

   }

}

   }

  

   /**

* Sorts an array into non-decreasing order. This method uses a selection

* sort algorithm, in which the largest item is found and placed at the end of

* the list, then the second-largest in the next to last place, and so on.

*/

   public static void selectionSort(int[] array) {

for (int top = array.length - 1; top >= 0; top--) {

   int positionOfMax = top;

   for (int i = 0; i <= top; i++) {

if (array[i] > array[positionOfMax])

   positionOfMax = i;

   }

   int temp = array[top]; // swap top item with biggest item

   array[top] = array[positionOfMax];

   array[positionOfMax] = temp;

}

   }

  

   /**

* Sorts an array into non-decreasing order. This method uses a standard

* insertion sort algorithm, in which each element in turn is moved downwards

* past any elements that are greater than it.

*/

   public static void insertionSort(int[] array) {

for (int top = 1; top < array.length; top++) {

   int temp = array[top]; // copy item that into temp variable

   int pos = top - 1;

   while (pos > 0 && array[pos] > temp) {

   // move items that are bigger than temp up one position

array[pos+1] = array[pos];

pos--;

   }

   array[pos] = temp; // place temp into last vacated position

}

   }

  

   /**

* Outputs the ints in an array on one line, separated by spaces,

* with a line feed at the end.

*/

   private static void printArray(int[] array) {

for (int i = 0; i < array.length; i++) {

   System.out.print(" ");

   System.out.print(array[i]);

}

System.out.println();

   }

}


===================================================================
SeE Output

178 1799 public static void main(String□ args) { 180 181 for int n 100; n<- 10000; n n*10) 182 183 Array of n elements E
Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble...

    Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...

  • C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort...

    C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort • Merge Sort • Quick Sort Select any two of the above sorting techniques: 1. Write an algorithm (pseudocode) for the two selected sorting techniques 2. Write two separate C++ programs using user defined functions for each of the selected sorting techniques.

  • Write a program that obtains the execution time of selection sort, radix sort, bubble sort, merge...

    Write a program that obtains the execution time of selection sort, radix sort, bubble sort, merge sort, quick sort, and heap sort for input size 50000, 100,000, 150,000, 200,000, 250,000, and 300,000. Your program should create data randomly and print a table like this: In the same program, obtain the execution time of selection sort, radix sort, bubble sort, and heap sort for input size 2,000,000, 3,000,000, 4,000,000, and 5,000,000. (Hint: You can use the code template below to obtain...

  • Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection...

    Using C++ Create a program that performs the Bubble Sort, the Insertion Sort, and the Selection Sort with arrays of varying lengths. You will time each algorithm. The algorithms' time complexities should allow us to predict how these algorithms will perform. Program needs Four separate arrays, each with the same length and filled with the same sequence of randomly chosen numbers. Four separate functions, one for each of the four algorithms. All four functions will need to be timed. Be...

  • Language is Java. Create an insertion, selection, quick, and merge sort algorithm that sorts 6 9...

    Language is Java. Create an insertion, selection, quick, and merge sort algorithm that sorts 6 9 8 12 3 1 7

  • 8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above...

    8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick...

    C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays of size 5000, 50000, and 500000. For...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT