Topic: Sorting
Questions Successfully Completed: 1
1) Quick Sort | Medium |
Quick Sort
Time Complexity : O(nlogn)
Space Complexity: O(logn)
Question
Input: N = 9 arr[] = { 2, 1, 6, 10, 4, 1, 3, 9, 7} Output: 1 1 2 3 4 6 7 9 10
public class quickSort {
public static int partition(int arr[],int l, int h){
int i = l;
int j = h;
int pivot = arr[l];
do{
do{i++;}while(arr[i]<=pivot);
do{j--;}while(arr[j]>pivot);
if(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}while(i<j);
int temp2 = arr[l];
arr[l] = arr[j];
arr[j] = temp2;
return j;
}
public static void funquicksort(int arr[],int l,int h){
int j;
if(l<h){
j = partition(arr,l,h);
funquicksort(arr,0,j);
funquicksort(arr,j+1,h);
}
}
public static void main(String[] args) {
int INT_MAX = Integer.MAX_VALUE;
System.out.println(INT_MAX);
int arr[] = {2,1,6,10,4,1,3,9,7,INT_MAX};
funquicksort(arr,0,9);
System.out.println(Arrays.toString(arr));
}
}
// OUTPUT
//2147483647
// [1, 1, 2, 3, 4, 6, 7, 9, 10, 2147483647]
Thank you for Reading! :)