Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added Improvised QuickSort Algorithm in JAVA #889

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,7 @@ About: Student of Bio Technology <br/>
Place: West Jakarta, Indonesia<br/>
About: Student of Software Enginering <br/>
</br>
</br>Name: [Nehansh](https://github.com/Nehansh10)<br/>
Place: Gurugram, Haryana<br/>
About: Student of Computer Science and Engineering <br/>
</br>
118 changes: 118 additions & 0 deletions Data Structures and Algorithms/Algorithms/java/ImprovedQuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import java.io.*;
import java.util.*;

//Improvized QuickSort Algorithm so that it works fast even on sequences conatining equal elements.

public class Sorting {
private static Random random = new Random();
//Code by Nehansh
public static int partition(int start_idx, int finish_idx, int[] a){
if(a.length == 0){
//Sometimes you might get an array with length 0;
return -1;
}
if(a.length == 1){
//Cathes exception if a length is one
return start_idx;
}
int beg_idx = start_idx +1;
int end_idx = finish_idx;

while (end_idx > beg_idx) {
//Begining index goes on until there is a number bigger than starting value
for (; beg_idx < end_idx && a[beg_idx] <= a[start_idx]; beg_idx++) {
}
//Ending index goes on until there is a number smaller than starting value
for (; beg_idx < end_idx && a[end_idx] > a[start_idx]; end_idx--) {
}


//Once both values are found, a swap happens
int temp = a[beg_idx];
a[beg_idx] = a[end_idx];
a[end_idx] = temp;
}



//Swap that happens at the end
if(a[beg_idx] < a[start_idx]){
int temp = a[beg_idx];
a[beg_idx] = a[start_idx];
a[start_idx] = temp;
return beg_idx;
}
int temp = a[beg_idx-1];
a[beg_idx-1] = a[start_idx];
a[start_idx] = temp;
return beg_idx-1;

}

public static void quicksort(int start_idx, int finish_idx, int[] a){
if(finish_idx - start_idx < 1 || start_idx < 0 ||finish_idx >= a.length){
return;
}

//Find the partiton index
int part = partition(start_idx, finish_idx, a);

int partx = part;


while(part>0 && a[part-1]==a[part]){
part--;
}
while(part<a.length && a[part]==a[part+1]){
partx++;
}


//partition both sides of the array
quicksort(start_idx, part-1, a);
quicksort(partx+1, finish_idx, a);
}

public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// randomizedQuickSort(a, 0, n - 1);
quicksort(0,n-1,a);
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}

static class FastScanner {
BufferedReader br;
StringTokenizer st;

FastScanner(InputStream stream) {
try {
br = new BufferedReader(new InputStreamReader(stream));
} catch (Exception e) {
e.printStackTrace();
}
}

String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}

int nextInt() {
return Integer.parseInt(next());
}
}
}