Question

Implement insertion, selection, merge and quick sorts. Perform a series of benchmarking tests to see which...

Implement insertion, selection, merge and quick sorts. Perform a series of benchmarking tests to see which one is faster. Your tests should include sequences that are “random” as well as ”almost” sorted. Language is Java. There should be a method for insertion, selection, merge, and quicksort. Each method will use long startingTime = System.currentTimeMillis();to count time for each sort.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

import java.util.Scanner;

public class Sorting {
   public static void main(String args[]) {
       int n;
       long startingTime,endingTime;
      
       Scanner in = new Scanner(System.in);
       System.out.print("Enter size of array : ");
       n = in.nextInt();
       int arr[] = new int[n];
       int temp[] = new int[n];
       int temp1[] = new int[n];
      
       System.out.print("Enter elements of array ");
       for(int i=0;i<n;i++) {
           arr[i] = in.nextInt();
           temp[i] = arr[i];
       }
      
       System.out.println("Insertion Sort");
       startingTime = System.currentTimeMillis();
       insertionSort(temp,n);
       endingTime = System.currentTimeMillis();
       System.out.println("Time Taken by Insertion Sort "+ (endingTime - startingTime)+" milliseconds");
      
      
       for(int i=0;i<n;i++) {
           temp[i] = arr[i];
       }
       System.out.println("\n\nSelection Sort");
       startingTime = System.currentTimeMillis();
       selectionSort(temp,n);
       endingTime = System.currentTimeMillis();
       System.out.println("Time Taken by selection Sort "+ (endingTime - startingTime)+" milliseconds");
      
      
       for(int i=0;i<n;i++) {
           temp[i] = arr[i];
       }
       System.out.println("\n\nMerge Sort");
       startingTime = System.currentTimeMillis();
       mergeSort(temp,temp1,0,n-1);
       endingTime = System.currentTimeMillis();
       System.out.println("Time Taken by merge Sort "+ (endingTime - startingTime)+" milliseconds");
      
       for(int i=0;i<n;i++) {
           temp[i] = arr[i];
       }
       System.out.println("\n\nQuick Sort");
       startingTime = System.currentTimeMillis();
       quickSort(temp,0,n-1);
       endingTime = System.currentTimeMillis();
       System.out.println("Time Taken by Quick Sort "+ (endingTime - startingTime)+" milliseconds");
      
       in.close();
   }
  
   //Merge function
       public static void merge(int a[],int temp[],int l,int m,int r){
           int temp_pos=l,size=r-l+1,left_end=m-1;
           while(l<=left_end&&m<=r){
               if(a[l]<a[m])temp[temp_pos++]=a[l++];
               else temp[temp_pos++]=a[m++];
           }
           while(l<=left_end)temp[temp_pos++]=a[l++];
           while(m<=r)temp[temp_pos++]=a[m++];
           for(int i=0;i<size;i++){
               a[r]=temp[r];
               r--;
           }
       }
       //mergesort function
       public static void mergeSort(int a[],int temp[],int l,int r){
           if(l<r){
               int mid=l+(r-l)/2;
               mergeSort(a,temp,l,mid);
               mergeSort(a,temp,mid+1,r);
               merge(a,temp,l,mid+1,r);
           }
       }
      
       public static void insertionSort(int a[],int n){
           for(int i=1;i<n;i++){
               int j=i-1,val=a[i];
               while(j>=0&&a[j]>val){
               a[j+1]=a[j];
               j--;}
               a[++j]=val;
           }
       }
       public static void selectionSort(int a[],int n){
           int min;
           for(int i=0;i<n-1;i++){
               min=i;
               for(int j=i+1;j<n;j++)if(a[j]<a[min])min=j;
               if(min!=i){
                   int temp=a[i];
                   a[i]=a[min];
                   a[min]=temp;
               }
           }
       }
      
       public static int findPivot(int arr[], int low, int high)
   {
   int pivot = arr[high];
   int i = (low-1);
   for (int j=low; j<high; j++)
   {
   if (arr[j] < pivot)
   {
   i++;
  
   int temp = arr[i];
   arr[i] = arr[j];
   arr[j] = temp;
   }
   }
     
   int temp = arr[i+1];
   arr[i+1] = arr[high];
   arr[high] = temp;
  
   return i+1;
   }
  
   public static void quickSort(int arr[], int low, int high)
   {
   if (low < high) {
   int pi = findPivot(arr, low, high);
   quickSort(arr, low, pi-1);
   quickSort(arr, pi+1, high);
   }
   }
}

Add a comment
Know the answer?
Add Answer to:
Implement insertion, selection, merge and quick sorts. Perform a series of benchmarking tests to see which...
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
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