forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSorting.java
More file actions
47 lines (37 loc) · 804 Bytes
/
QuickSorting.java
File metadata and controls
47 lines (37 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.sort;
public class QuickSorting {
static void QuickSort(int[] arr, int low, int high){
if(low>high) return;
int mid= low+(high-low)/2;
int pivot= arr[mid];
int i=low;
int j=high;
while(i<=j){
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<=j){
int temp= arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}
if(low<j)
QuickSort(arr, low, j);
if(high>i)
QuickSort(arr, i, high);
}
public static void main(String[] args) {
int[] arr={1,50,30,10,60,80};
System.out.println("Before Sort");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i] +"\t");
QuickSort(arr, 0, arr.length-1);
System.out.println("\nAfter Sort");
for(int i=0;i<arr.length;i++)
System.out.print(arr[i] +"\t");
}
}