Skip to content

Commit bc8ef54

Browse files
Merge pull request #243 from abhishektripathi66/codespace-solid-space-dollop-7p99x4q66443w575
adding the sorting of array in different ways
2 parents 3b34433 + 3b65243 commit bc8ef54

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package codingquestions.problemsonsorting.quicksort;
2+
3+
import java.util.Arrays;
4+
5+
public class SortArrayWithThreeTypes {
6+
7+
public static void main(String[] args) {
8+
int[] arr = {0,1,2,1,1,2};
9+
Sort0s1s2s(arr);
10+
System.out.println(Arrays.toString(arr));
11+
int[] arr1 = {2,1,2,20,10,20,1};
12+
Sort3Way(arr1,2);
13+
System.out.println(Arrays.toString(arr1));
14+
int[] arr2 = {10,5,6,3,20,9,40};
15+
int[] range ={5,10};
16+
SortaroundRange(arr2,range);
17+
System.out.println(Arrays.toString(arr2));
18+
19+
}
20+
// Dutch National Flag Algorithm is the efficient Solution
21+
public static void Sort0s1s2s(int[] arr){
22+
int low=0,high=arr.length-1,mid=0;
23+
while(mid<=high){
24+
if(arr[mid]==0){
25+
arr[mid]=arr[low];
26+
arr[low]=0;
27+
low++;mid++;
28+
}
29+
else if(arr[mid]==1){
30+
mid++;
31+
}
32+
else{
33+
int temp = arr[mid];
34+
arr[mid]=arr[high];
35+
arr[high]=temp;
36+
high--;
37+
}
38+
}
39+
}
40+
41+
public static void Sort3Way(int[] arr,int pivot){
42+
int low=0,high=arr.length-1,mid=0;
43+
while(mid<=high){
44+
if(arr[mid]<pivot){
45+
int temp = arr[mid];
46+
arr[mid]=arr[low];
47+
arr[low]=temp;
48+
low++;mid++;
49+
}
50+
else if(arr[mid]==pivot){
51+
mid++;
52+
}
53+
else{
54+
int temp = arr[mid];
55+
arr[mid]=arr[high];
56+
arr[high]=temp;
57+
high--;
58+
}
59+
}
60+
}
61+
62+
public static void SortaroundRange(int[] arr,int[] range){
63+
int low=0,high=arr.length-1,mid=0;
64+
while(mid<=high){
65+
if(arr[mid]<range[0]){
66+
int temp = arr[mid];
67+
arr[mid]=arr[low];
68+
arr[low]=temp;
69+
low++;mid++;
70+
}
71+
else if(arr[mid]>=range[0] && arr[mid]<=range[1]){
72+
mid++;
73+
}
74+
else{
75+
int temp = arr[mid];
76+
arr[mid]=arr[high];
77+
arr[high]=temp;
78+
high--;
79+
}
80+
}
81+
}
82+
83+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package codingquestions.problemsonsorting.quicksort;
2+
3+
import java.util.Arrays;
4+
5+
public class SortArrayWithTwoTypes {
6+
7+
public static void main(String[] args) {
8+
int[] arr = {-12,18,-10,15};
9+
sortPositiveAndNegative(arr);
10+
System.out.println(Arrays.toString(arr));
11+
int[] arr1 = {15,14,13,12};
12+
sortEvenandOdd(arr1);
13+
System.out.println(Arrays.toString(arr1));
14+
int[] arr2 = {0,1,1,1,0,0,1,0,0,0,1,1,1,0,0,0,1,0,1,0,1,0};
15+
sortBinaryArray(arr2);
16+
System.out.println(Arrays.toString(arr2));
17+
}
18+
19+
public static void sortPositiveAndNegative(int[] arr){
20+
int i=-1,j=arr.length;
21+
while(true){
22+
do{i++;}while(arr[i]<0);
23+
do{j--;}while(arr[j]>=0);
24+
if(i>=j) return;
25+
int temp = arr[i];
26+
arr[i]=arr[j];
27+
arr[j]=temp;
28+
}
29+
}
30+
31+
public static void sortEvenandOdd(int[] arr){
32+
int i=-1,j=arr.length;
33+
while(true){
34+
do{i++;}while(arr[i]%2==0);
35+
do{j--;}while(arr[j]%2!=0);
36+
if(i>=j) return;
37+
int temp = arr[i];
38+
arr[i]=arr[j];
39+
arr[j]=temp;
40+
41+
}
42+
}
43+
44+
public static void sortBinaryArray(int[] arr){
45+
int i=-1,j=arr.length;
46+
while(true){
47+
do{i++;}while(arr[i]==0);
48+
do{j--;}while(arr[j]%2==1);
49+
if(i>=j) return;
50+
int temp = arr[i];
51+
arr[i]=arr[j];
52+
arr[j]=temp;
53+
54+
}
55+
}
56+
57+
}

0 commit comments

Comments
 (0)