Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Sorting Algorithms/Quick Sort/JavaImplementation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
public class QuickSort {
static int n=10;
static int a[]={5, 3, 12, 78, 32, 51, 16, 75, 8, 33};
public static void main(String[] args) {
QuickAlg(0,n-1);
DisplayArray();
}
public static void QuickAlg(int p, int r) {
int i,j,temp;
i=p+1;
j=r;
if(p<r) {
while(true) {
while(a[i]<a[p] && i<r)
i++;
while(a[j]>a[p])
j--;
if(i<j) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
break;
}
temp=a[p];
a[p]=a[j];
a[j]=temp;
QuickAlg(p,j-1);
QuickAlg(j+1,r);
}
}
public static void DisplayArray() {
System.out.println("Sorted array: ");
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
}
}
11 changes: 11 additions & 0 deletions Sorting Algorithms/Quick Sort/Theory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Quicksort

Quicksort uses the divide-and-conquer algorithmic technique to sort elements. It is an in-place sorting algorithm but it is not stable. In its best and average case, it has a time complexity of O(nlogn). Its worst case time complexity is O(n^2).

#### Steps for Quicksort:

- Choose an element ('pivot').
- Rearrange the array until a point of division is found such that when pivot is placed there, elements to the left are smaller than pivot and elements to the right are greater than pivot. This will also be the correct postion of pivot in the final sorted array.
- Move pivot to that position.
- This splits the array into two subarrays.
- Apply these steps to the two subarrays recursively until the entire array is sorted.