Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Quicksort{
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter size");
- int n = sc.nextInt();
- int arr[] = new int[n];
- System.out.println("Enter the array");
- for(int x = 0; x<n; x++){
- arr[x] = sc.nextInt();
- }
- Quicksort q = new Quicksort();
- q.sort(arr, 0, n-1);
- System.out.println(Arrays.toString(arr));
- }
- int partition(int arr[], int low, int high){
- int temp;
- int pivot = arr[high];
- int x = low - 1;
- for(int y = low; y<high; y++){
- if(arr[y]<pivot){
- x = x + 1;
- temp = arr[y];
- arr[y] = arr[x];
- arr[x] = temp;
- }
- }
- temp = arr[x+1];
- arr[x+1] = arr[high];
- arr[high] = temp;
- return (x+1);
- }
- void sort(int arr[], int low, int high){
- if(low < high){
- int pivot = partition(arr, low, high);
- sort(arr, low, pivot-1);
- sort(arr, pivot+1, high);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment