Question

Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort...

Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms.

Compute the CPU processing time for all the algorithms for varying input sizes as follows: N = 102, 103, 104, 105, and 106

Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 103, 1 - 106, 1 – 109, 1 - 1012

Write down your results as a table (with varying input size and algorithm type) and plot the graph. Present your inferences and/or explanations for your result (in a separate document).

Homework Answers

Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

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

Kindly revert for any queries

Thanks.

Know the answer?
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
Similar Questions
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI input message, then prompt user to enter numbers in GUI input message also, sort these numbers in descending order using Selected sorting algorithm, and show the result in message dialog as shown in the following figure. The program contains two classes: 1- “Sort” Class which contains 3 methods named as follow: a. InsertionSort(int[] numbers), returns sorted numbers from the passed array using Insertion sort...
Write a MATLAB program that implements the Insertion Sort algorithm. Test your implementation with this set...
Write a MATLAB program that implements the Insertion Sort algorithm. Test your implementation with this set of inputs: 16.2 32.7 -0.5 4.4 21.8 -17.0 6.9 14.1 1.5.
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT