Skip to content

Commit 7a06623

Browse files
committed
Solution to sort Array using Quick Sort
1 parent e591241 commit 7a06623

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

src/Sorting/SortingProblems.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ public class SortingProblems {
88
static int count = 0;
99
public static void main(String[] args) {
1010

11-
int [] array = {45, 10, 15, 25, 50};
11+
// int [] array = {45, 10, 15, 25, 50};
12+
int [] array = {5, 3, 1, 9, 4};
13+
quickSort(array,0,array.length-1);
14+
System.out.println(Arrays.toString(array));
15+
16+
int count = insertionSort(array);
17+
System.out.println(count);
18+
1219
mergeSortInPlace(array,0,array.length-1);
1320

1421
System.out.println("The total count is - " + count);
@@ -37,9 +44,62 @@ public static void main(String[] args) {
3744
System.out.println(Arrays.toString(answerArray));
3845
// int [] A = {-4, 7, 5, 3, 5, -4, 2, -1, -9, -8, -3, 0, 9, -7, -4, -10, -4, 2, 6, 1, -2, -3, -1, -8, 0, -8, -7, -3, 5, -1, -8, -8, 8, -1, -3, 3, 6, 1, -8, -1, 3, -9, 9, -6, 7, 8, -6, 5, 0, 3, -4, 1, -10, 6, 3, -8, 0, 6, -9, -5, -5, -6, -3, 6, -5, -4, -1, 3, 7, -6, 5, -8, -5, 4, -3, 4, -6, -7, 0, -3, -2, 6, 8, -2, -6, -7, 1, 4, 9, 2, -10, 6, -2, 9, 2, -4, -4, 4, 9, 5, 0, 4, 8, -3, -9, 7, -8, 7, 2, 2, 6, -9, -10, -4, -9, -5, -1, -6, 9, -10, -1, 1, 7, 7, 1, -9, 5, -1, -3, -3, 6, 7, 3, -4, -5, -4, -7, 9, -6, -2, 1, 2, -1, -7, 9, 0, -2, -2, 5, -10, -1, 6, -7, 8, -5, -4, 1, -9, 5, 9, -2, -6, -2, -9, 0, 3, -10, 4, -6, -6, 4, -3, 6, -7, 1, -3, -5, 9, 6, 2, 1, 7, -2, 5};
3946
int result = nobleInteger(A);
47+
4048
System.out.println(result);
4149
}
4250

51+
static void quickSort(int [] array, int start, int end){
52+
53+
if (start >= end) return;
54+
55+
int p = rearrange(array,start,end);
56+
quickSort(array,start,p-1);
57+
quickSort(array,p+1,end);
58+
}
59+
60+
static int rearrange(int [] array, int start, int end){
61+
int p1 = start+1; int p2 = end;
62+
63+
while (p1 <= p2){
64+
if (array[p1] <= array[start]){
65+
p1++;
66+
} else if (array[p2] > array[start]) {
67+
p2--;
68+
}else {
69+
int temp = array[p1];
70+
array[p1] = array[p2];
71+
array[p2] = temp;
72+
p1++;
73+
p2--;
74+
}
75+
}
76+
77+
int temp = array[start];
78+
array[start] = array[p2];
79+
array[end] = temp;
80+
81+
return p2;
82+
}
83+
84+
static int insertionSort(int [] A){
85+
int count = 0;
86+
for(int i = 1; i < A.length;i++){
87+
for(int j = i-1; j >= 0; j--){
88+
if(A[j] > A[j+1]){
89+
count++;
90+
int temp = A[j];
91+
A[j] = A[j+1];
92+
A[j+1] = temp;
93+
int ans = A[i] * (1 << i);
94+
System.out.println("Count is " + count + " when i is " + i + " " + Arrays.toString(A));
95+
}else{
96+
break;
97+
}
98+
}
99+
}
100+
return count;
101+
}
102+
43103
static double average(int[] salary) {
44104

45105
Arrays.sort(salary);

0 commit comments

Comments
 (0)