Question

i need a java program that reads text from a file and compare between 4 sorting...

i need a java program that reads text from a file and compare between 4 sorting algorithms (insertion sort , merge sort , heap sort and quick sort ) according to time taken for each to sort it
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* This code takes an input from the file data.txt and compares the time for all the 4 sorts i.e insertion,merge,quick and heap sort
the data.txt should have all the nos in a single line and they should be comma seperated i.e the file should have them like:-
12,23,35,32,43,21,43,23
*/


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Compare_sorts
{  
   static String line;
   public static void main(String args[])
   {
       readFile();
       System.out.println("the array read is:");
       System.out.println(line);
       String tokens[]=line.split(",");
       int heap_sort_numbers[]=new int[tokens.length];
       int insertion_sort_numbers[]=new int[tokens.length];
       int merge_sort_numbers[]=new int[tokens.length];
       int quick_sort_numbers[]=new int[tokens.length];
       for (int i=0;i<tokens.length;i++)
       {
           heap_sort_numbers[i]=Integer.parseInt(tokens[i]);
           insertion_sort_numbers[i]=Integer.parseInt(tokens[i]);
           merge_sort_numbers[i]=Integer.parseInt(tokens[i]);
           quick_sort_numbers[i]=Integer.parseInt(tokens[i]);
           //System.out.println(numbers[i]+2);
       }  
       //print_array(heap_sort_numbers);
       //print_array(insertion_sort_numbers);
       //print_array(merge_sort_numbers);
       //print_array(quick_sort_numbers);
       //System.out.println("time in nanoseconds is "+System.nanoTime());
         long time_nano1,time_nano2;
        
        
         time_nano1=System.nanoTime();
         heap_sort(heap_sort_numbers);
       time_nano2=System.nanoTime();
       System.out.println("time required for heap sort in nanoseconds is "+(time_nano2-time_nano1));
       print_array(heap_sort_numbers);
      
      
       time_nano1=System.nanoTime();
         insertion_sort(insertion_sort_numbers);
       time_nano2=System.nanoTime();
       System.out.println("time required for insertion sort in nanoseconds is "+(time_nano2-time_nano1));
       print_array(insertion_sort_numbers);
      
      
       time_nano1=System.nanoTime();
         merge_sort(merge_sort_numbers,0,merge_sort_numbers.length-1);
       time_nano2=System.nanoTime();
       System.out.println("time required for merge sort in nanoseconds is "+(time_nano2-time_nano1));
       print_array(merge_sort_numbers);
      
       time_nano1=System.nanoTime();
         quick_sort(quick_sort_numbers,0,quick_sort_numbers.length-1);
       time_nano2=System.nanoTime();
       System.out.println("time required for quick sort in nanoseconds is "+(time_nano2-time_nano1));
       print_array(quick_sort_numbers);
      
      
      
   }
   public static void print_array(int array[])
   {
       for(int i=0;i<array.length;i++)
       {
           System.out.print(array[i]+",");
       }
       System.out.print("\n");
   }
   public static void readFile()
   {
       BufferedReader reader;
       try
       {
           reader=new BufferedReader(new FileReader("data.txt"));
           line=reader.readLine();
          
           reader.close();
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
   }
  
  
  
  
  
  
  
  
   public static void heap_sort(int arr[])
    {
        int n = arr.length;

        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--)
            heapify(arr, n, i);

        // One by one extract an element from heap
        for (int i=n-1; i>=0; i--)
        {
            // Move current root to end
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            // call max heapify on the reduced heap
            heapify(arr, i, 0);
        }
    }

    // To heapify a subtree rooted with node i which is
    // an index in arr[]. n is size of heap
    static void heapify(int arr[], int n, int i)
    {
        int largest = i; // Initialize largest as root
        int l = 2*i + 1; // left = 2*i + 1
        int r = 2*i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest])
            largest = l;

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest])
            largest = r;

        // If largest is not root
        if (largest != i)
        {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;

            // Recursively heapify the affected sub-tree
            heapify(arr, n, largest);
        }
    }
  
  
    /*quick sort*/
    static int partition(int arr[], int low, int high)
    {
        int pivot = arr[high];
        int i = (low-1); // index of smaller element
        for (int j=low; j<high; j++)
        {
            // If current element is smaller than or
            // equal to pivot
            if (arr[j] <= pivot)
            {
                i++;

                // swap arr[i] and arr[j]
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        // swap arr[i+1] and arr[high] (or pivot)
        int temp = arr[i+1];
        arr[i+1] = arr[high];
        arr[high] = temp;

        return i+1;
    }


    /* The main function that implements QuickSort()
      arr[] --> Array to be sorted,
      low --> Starting index,
      high --> Ending index */
    static void quick_sort(int arr[], int low, int high)
    {
        if (low < high)
        {
            /* pi is partitioning index, arr[pi] is
              now at right place */
            int pi = partition(arr, low, high);

            // Recursively sort elements before
            // partition and after partition
            quick_sort(arr, low, pi-1);
            quick_sort(arr, pi+1, high);
        }
    }
  
  
    static void insertion_sort(int arr[])
    {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            int key = arr[i];
            int j = i - 1;

            /* Move elements of arr[0..i-1], that are
               greater than key, to one position ahead
               of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
  
  
    /*Merge Sort*/
    static void merge(int arr[], int l, int m, int r)
    {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        int L[] = new int [n1];
        int R[] = new int [n2];

        /*Copy data to temp arrays*/
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];


        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarry array
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    static void merge_sort(int arr[], int l, int r)
    {
        if (l < r)
        {
            // Find the middle point
            int m = (l+r)/2;

            // Sort first and second halves
            merge_sort(arr, l, m);
            merge_sort(arr , m+1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }
  
  
   
}

Add a comment
Know the answer?
Add Answer to:
i need a java program that reads text from a file and compare between 4 sorting...
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