m2skills

qs java

Sep 10th, 2017
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * Created by MOHIT on 10-09-2017.
  7.  */
  8.  
  9.  // Program to implement quick sort in java
  10. public class quick_sort{
  11.     public static void main(String arg[]){
  12.        
  13.         // accepting string as comma seperated inputs
  14.         String input_string;
  15.         ArrayList<Integer> myList1 = new ArrayList<Integer>();
  16.         Scanner sc = new Scanner(System.in);
  17.         System.out.print("Enter the Array to be Sorted : ");
  18.         input_string = sc.next();
  19.         String[] temp = input_string.split(",");
  20.        
  21.         // adding those integers to array list
  22.         for (String t:temp) {
  23.             myList1.add(Integer.parseInt(t));
  24.         }
  25.        
  26.         // print the list
  27.         System.out.println("The list before sorting is : ")
  28.         for (int temp2:myList1) {
  29.             System.out.print(temp2 + "\t");
  30.         }
  31.        
  32.         // calling the quick sort function with first position as 0 and last position as list size -1
  33.         myList1 = Quick_Sort(myList1, 0, myList1.size()-1);
  34.  
  35.         // printing the sorted list
  36.         System.out.println("\nThe Sorted List is : ");
  37.         // displaying the sorted list
  38.         for (int temp2:myList1) {
  39.             System.out.print(temp2 + "\t");
  40.         }
  41.     }
  42.  
  43.     // recursive function to implement quick sort
  44.     static ArrayList<Integer> Quick_Sort(ArrayList<Integer> data, int first, int last)
  45.     {
  46.         int pivot, left_mark, right_mark;
  47.  
  48.         // checking if the list has more than one number
  49.         if(first < last)
  50.         {
  51.             // initialise variables
  52.             pivot = first;
  53.             left_mark = first;
  54.             right_mark = last;
  55.  
  56.             // loop while left marker is less than right marker
  57.             while(left_mark < right_mark)
  58.             {
  59.                 // while the data at the left mark is less than pivot keep incrementing the left_mark
  60.                 while(data.get(left_mark) <= data.get(pivot) && left_mark < right_mark)
  61.                     left_mark++;
  62.  
  63.                 // while the data at the right mark is greater than pivot keep decrementing the right_mark
  64.                 while(data.get(right_mark) >= data.get(pivot) && left_mark <= right_mark)
  65.                     right_mark--;
  66.  
  67.                 // swap the elements if right mark is greater than left mark
  68.                 if(right_mark > left_mark)
  69.                 {
  70.                     Collections.swap(data, left_mark, right_mark);
  71.                 }
  72.  
  73.                 // finally swap the pivot and the right mark element
  74.                 Collections.swap(data, pivot, right_mark);
  75.             }
  76.  
  77.             // make recursive calls to the function
  78.             Quick_Sort(data, first, right_mark-1);
  79.             Quick_Sort(data, right_mark+1, last);
  80.         }
  81.        
  82.         // returning the sorted list
  83.         return data;
  84.     }
  85.  
  86. }
Add Comment
Please, Sign In to add comment