Skip to content

Commit fc5e1e7

Browse files
committed
implemented quicksort
1 parent c70f0b1 commit fc5e1e7

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package algorithms;
2+
3+
import java.util.ArrayList;
4+
5+
public class quick extends sorting_algorithm
6+
{
7+
public quick()
8+
{
9+
super();
10+
}
11+
12+
public quick(ArrayList<Integer> source)
13+
{
14+
super(source);
15+
}
16+
17+
@Override
18+
public ArrayList<Integer> sort_out()
19+
{
20+
sorting_procedure(0, to_be_sorted.size() - 1);
21+
22+
return to_be_sorted;
23+
}
24+
25+
void sorting_procedure(int low, int high)
26+
{
27+
int pivot = to_be_sorted.get((low + high) / 2);
28+
int i = low, j = high;
29+
30+
do{
31+
while (to_be_sorted.get(i) < pivot) i++;
32+
while (to_be_sorted.get(j) > pivot) j--;
33+
if (i <= j)
34+
{
35+
swap(i, j);
36+
i++;
37+
j--;
38+
}
39+
} while (i <= j);
40+
41+
if (j > low) sorting_procedure(low, j);
42+
if (i < high) sorting_procedure(i, high);
43+
}
44+
}

searching_methods/src/algorithms/sorting_algorithm.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public abstract class sorting_algorithm
66
{
77
ArrayList<Integer> to_be_sorted;
88

9-
public sorting_algorithm()
9+
sorting_algorithm()
1010
{
1111
this.to_be_sorted = new ArrayList<>();
1212
}
1313

14-
public sorting_algorithm(ArrayList<Integer> to_be_sorted)
14+
sorting_algorithm(ArrayList<Integer> to_be_sorted)
1515
{
1616
this.to_be_sorted = new ArrayList<>(to_be_sorted.size());
1717
this.to_be_sorted.addAll(to_be_sorted);

searching_methods/src/testing/main.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class main
99
{
10-
public static Random seed = new Random();
10+
static Random seed = new Random();
1111
static ArrayList<Integer> randomize_new_array(int size)
1212
{
1313
ArrayList<Integer> new_array = new ArrayList<Integer>(size);
@@ -17,9 +17,8 @@ static ArrayList<Integer> randomize_new_array(int size)
1717

1818
static void print_array_out(ArrayList<Integer> array)
1919
{
20-
int size = array.size();
21-
for (int i = 0; i < size; i++)
22-
System.out.println(array.get(i));
20+
for (Integer integer : array)
21+
System.out.println(integer);
2322

2423
System.out.println("🧶-----------------------🐈");
2524
}
@@ -46,8 +45,11 @@ public static void main(String args[])
4645

4746
bubble bubble_sort = new bubble();
4847
insert insert_sort = new insert();
48+
quick quick_sort = new quick();
4949

5050
System.out.println(estimate_sorting_duration(bubble_sort, tests));
5151
System.out.println(estimate_sorting_duration(insert_sort, tests));
52+
System.out.println(estimate_sorting_duration(quick_sort, tests));
53+
// System.out.println(estimate_sorting_duration(quick_sort, tests));
5254
}
5355
}

0 commit comments

Comments
 (0)