Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * Created by krngrvr09 on 31/1/15.
- */
- public class QuickSort {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- int number_of_numbers = scanner.nextInt();
- int[] inputs = new int[number_of_numbers];
- for(int i=0;i<number_of_numbers;i++){
- inputs[i] = scanner.nextInt();
- }
- quickSort(inputs,0,number_of_numbers-1);
- for(int i=0;i<number_of_numbers;i++){
- System.out.print(inputs[i]+" ");
- }
- }
- public static void quickSort(int[] inputs, int start, int end){
- if(start>=end){
- return;
- }
- else {
- int new_pivot = partition(inputs, start, end);
- quickSort(inputs, start, new_pivot - 1);
- quickSort(inputs, new_pivot + 1, end);
- }
- }
- public static int partition(int[] inputs,int start, int end){
- int pivot = inputs[end];
- int partition_index = start;
- for(int i=start;i<end;i++){
- if(inputs[i]<pivot){
- int temp = inputs[i];
- inputs[i] = inputs[partition_index];
- inputs[partition_index] = temp;
- partition_index++;
- }
- }
- int temp = inputs[partition_index];
- inputs[partition_index] = inputs[end];
- inputs[end] = temp;
- return partition_index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement