Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fee9c25

Browse files
bubble sort and binary sort added.
1 parent 71f6294 commit fee9c25

File tree

11 files changed

+240
-39
lines changed

11 files changed

+240
-39
lines changed

‎sorting_img/bubble_sort.PNG

34.4 KB
Loading[フレーム]

‎sorting_img/direct_insert_sorting.PNG

-45.3 KB
Binary file not shown.

‎sorting_img/insertion_sort.PNG

45.3 KB
Loading[フレーム]
File renamed without changes.

‎src/search/BinarySearch.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,48 @@
88
/**Binary search works only for sorted list of items.*/
99
public class BinarySearch {
1010

11-
static int search(double[] numbers, double value){
12-
boolean found = false;
11+
static boolean getSearchResult(double[] numbers, int from, int to, double value){
1312
int round = 0;
14-
int start = 0;
15-
int end = numbers.length - 1;
16-
int middle = 0;
13+
int start = from;
14+
int end = to;
15+
16+
while(start <= end){
17+
int middle = start + (end - start) / 2;
1718

18-
while(!found){
19-
middle = (end + start) / 2;
2019
showStep(numbers, start, end, middle, round);
2120

2221
if(numbers[middle] == value)
23-
found = true;
22+
return true;
2423
else if(numbers[middle] < value)
2524
start = middle + 1;
26-
else if(numbers[middle] > value)
25+
else /**if(numbers[middle] > value)*/
2726
end = middle - 1;
2827

2928
round++;
3029
}
30+
return false;
31+
}
32+
33+
static int getSearchIndex(double[] numbers, int from, int to, double value){
34+
int round = 0;
35+
int start = from;
36+
int end = to;
37+
38+
while(start <= end){
39+
int middle = start + (end - start) / 2;
40+
41+
showStep(numbers, start, end, middle, round);
3142

32-
return middle;
43+
if(numbers[middle] == value)
44+
return middle;
45+
else if(numbers[middle] < value)
46+
start = middle + 1;
47+
else /**if(numbers[middle] > value)*/
48+
end = middle - 1;
49+
50+
round++;
51+
}
52+
return -1;
3353
}
3454
static void showStep(double[] numbers, int start, int end, int mid, int round) {
3555
if(round == 0)
@@ -39,10 +59,7 @@ static void showStep(double[] numbers, int start, int end, int mid, int round) {
3959
System.out.println("------------------------------------------------------------------------------------------------------------------");
4060
}
4161

42-
public static void main(String[] args) {
43-
44-
double numbers[] = {-11, 0, 6, 10, 14, 20, 50, 73, 92, 100, 255};
45-
62+
static void showList(double[] numbers){
4663
/**print the sorted array*/
4764
System.out.print("Sorted List:{ ");
4865
for (int i = 0; i < numbers.length - 1; i++)
@@ -53,9 +70,21 @@ public static void main(String[] args) {
5370
for (int i = 0; i < numbers.length - 1; i++)
5471
System.out.print(i + " ");
5572
System.out.println(numbers.length - 1);
73+
}
74+
75+
public static void main(String[] args) {
76+
77+
double numbers[] = {-11, 0, 6, 10, 14, 20, 50, 73, 92, 100, 255};
78+
double value = -11;
79+
80+
showList(numbers);
5681

5782
/**Execute the search algorithm*/
58-
search(numbers, 0);
83+
boolean searchResult = getSearchResult(numbers, 0, numbers.length, value);
84+
System.out.println("found " + value + "? : " + searchResult);
85+
86+
int searchIndex = getSearchIndex(numbers, 0, numbers.length, value);
87+
System.out.println("found " + value + " at : " + searchIndex);
5988

6089
}
6190

‎src/sorting/BubbleSort.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package sorting;
2+
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4+
* Author: NJAD NISSI 安杰 *
5+
* COMPUTER SCIENCE ENGINEER *
6+
* PROGRAMMER IN C, CPP, JAVA, PYTHON. *
7+
* contact me at: njadnissi@gmail.com *
8+
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9+
/**
10+
* Bubble Sort:
11+
* (1) We start with the second element, and traverse the whole list,
12+
* looking for the right position for that (according to ascending or descending order).
13+
* (2) We repeat the process until the last iteration which is item the last item.
14+
* (#) the sorting is successful after n-1 times, n being the number of items.
15+
* */
16+
17+
public class BubbleSort {
18+
19+
static void sortAscendingOrder(double[] numbers) {
20+
System.out.println("BUBBLE SORT");
21+
showStep(numbers, 0);
22+
for (int i = 1; i < numbers.length; i++) {
23+
for (int j = 0; j < numbers.length; j++)
24+
if (numbers[i] < numbers[j])
25+
swap(numbers, i, j);
26+
showStep(numbers, i);
27+
}
28+
}
29+
30+
static void sortDescendingOrder(double[] numbers) {
31+
System.out.println("BUBBLE SORT");
32+
showStep(numbers, 0);
33+
for (int i = 1; i < numbers.length; i++) {
34+
for (int j = 0; j < numbers.length; j++)
35+
if (numbers[i] < numbers[j])
36+
swap(numbers, i, j);
37+
showStep(numbers, i);
38+
}
39+
}
40+
41+
static void swap(double[] numbers, int i, int j) {
42+
if (i != j) {
43+
numbers[i] += numbers[j];
44+
numbers[j] = numbers[i] - numbers[j];
45+
numbers[i] -= numbers[j];
46+
}
47+
}
48+
49+
static void showStep(double[] numbers, int round) {
50+
System.out.print("round" + round + "=> ");
51+
for (int j = 0; j < numbers.length - 1; j++)
52+
System.out.print(numbers[j] + " ");
53+
System.out.println(numbers[numbers.length - 1]);
54+
System.out.println("------------------------------------------------------------------------------------------------------------------");
55+
}
56+
57+
public static void main(String[] args) {
58+
59+
double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0};
60+
61+
sortAscendingOrder(numbers);
62+
sortDescendingOrder(numbers);
63+
64+
}
65+
}

‎src/sorting/DirectSorting.java renamed to ‎src/sorting/DirectInsertionSort.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
* PROGRAMMER IN C, CPP, JAVA, PYTHON. *
77
* contact me at: njadnissi@gmail.com *
88
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9-
public class DirectSorting {
9+
/**
10+
* Insertion Sort or direct Insertion Sort:
11+
* (1) We divide the list into two parts: Sorted space () and unsorted space [].
12+
* (2) we select the first item of the unsorted space and insert it into the right
13+
* position (according to ascending or descending order) in the sorted space.
14+
* (3) Repeat the process until the unsorted space has no item left.
15+
* */
16+
public class DirectInsertionSort {
1017

11-
static void sort(double[] numbers) {
18+
static void sortAscendingOrder(double[] numbers) {
1219
int sortedElNo = 1; /**directly push the first element in the sorted space*/
1320

1421
System.out.println("DIRECT INSERT SORTING\n(SORTED SPACE) [UNSORTED SPACE]");
@@ -23,7 +30,28 @@ static void sort(double[] numbers) {
2330
break;
2431
}
2532
}
26-
showStep(numbers, sortedElNo - 1, i);
33+
showStep(numbers, sortedElNo - 1, i+1);
34+
35+
sortedElNo++;
36+
}
37+
}
38+
39+
static void sortDescendingOrder(double[] numbers) {
40+
int sortedElNo = 1; /**directly push the first element in the sorted space*/
41+
42+
System.out.println("DIRECT INSERT SORTING\n(SORTED SPACE) [UNSORTED SPACE]");
43+
showStep(numbers, -1, 0);
44+
45+
for (int i = 0; i < numbers.length; i++) {
46+
47+
/**Move the current value from unsorted space to sorted space.*/
48+
for (int j = 0; j < sortedElNo - 1; j++) {
49+
if (numbers[i] > numbers[j]) {
50+
moveValue(numbers, i, j);
51+
break;
52+
}
53+
}
54+
showStep(numbers, sortedElNo - 1, i+1);
2755

2856
sortedElNo++;
2957
}
@@ -60,7 +88,8 @@ public static void main(String[] args) {
6088

6189
double[] numbers = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0};
6290

63-
sort(numbers);
91+
sortAscendingOrder(numbers);
92+
sortDescendingOrder(numbers);
6493

6594
}
6695

‎src/sorting/QuickSort.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sorting;
2+
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4+
* Author: NJAD NISSI 安杰 *
5+
* COMPUTER SCIENCE ENGINEER *
6+
* PROGRAMMER IN C, CPP, JAVA, PYTHON. *
7+
* contact me at: njadnissi@gmail.com *
8+
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9+
public class QuickSort {
10+
}

‎src/sorting/RadixSort.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sorting;
2+
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4+
* Author: NJAD NISSI 安杰 *
5+
* COMPUTER SCIENCE ENGINEER *
6+
* PROGRAMMER IN C, CPP, JAVA, PYTHON. *
7+
* contact me at: njadnissi@gmail.com *
8+
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9+
public class RadixSort {
10+
11+
static void sort(double[] numbers) {
12+
13+
}
14+
15+
public static void main(String[] args) {
16+
17+
double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0};
18+
19+
sort(numbers);
20+
21+
}
22+
}

‎src/sorting/SimpleSelectionSort.java renamed to ‎src/sorting/SelectionSort.java

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@
55
* COMPUTER SCIENCE ENGINEER *
66
* PROGRAMMER IN C, CPP, JAVA, PYTHON. *
77
* contact me at: njadnissi@gmail.com *
8-
** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9-
public class SimpleSelectionSort {
10-
11-
static void sort(double[] numbers) {
8+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9+
/**
10+
* Selection Sort:
11+
* (1) We divide the list into two parts: Sorted space () and unsorted space [].
12+
* (2) we select the minimum(for ascending order) or maximum(for descending order)
13+
* item from the unsorted space and move it to next position int the sorted space.
14+
* (3) Repeat the process until the unsorted space has no item left.
15+
* (#) the sorting is successful after n-1 times, n being the number of items.
16+
* */
17+
public class SelectionSort {
18+
19+
static void sortAscendingOrder(double[] numbers) {
1220
int sortedElementsNo = 0;
1321

1422
System.out.println("SIMPLE SELECT SORTING\n(SORTED SPACE) [UNSORTED SPACE]");
@@ -27,23 +35,23 @@ static void sort(double[] numbers) {
2735
}
2836
}
2937

30-
static void showStep(double[] numbers, int sortedElNo, int round) {
38+
static void sortDescendingOrder(double[] numbers) {
39+
int sortedElementsNo = 0;
3140

32-
System.out.print("round" + round + "=>( ");/**OPEN SORTED SPACE*/
33-
if (sortedElNo < 0) {
34-
System.out.print(" ) [ ");
35-
for (int j = 0; j < numbers.length - 1; j++)
36-
System.out.print(numbers[j] + " ");
37-
} else {
38-
for (int j = 0; j < numbers.length - 1; j++) {
39-
if (j == sortedElNo)
40-
System.out.print(numbers[j] + " ) [ ");/**CLOSE SORTED SPACE*/
41-
else
42-
System.out.print(numbers[j] + " ");
43-
}
41+
System.out.println("SIMPLE SELECT SORTING\n(SORTED SPACE) [UNSORTED SPACE]");
42+
showStep(numbers, -1, 0);
43+
44+
for (int round = 1; round <= numbers.length; round++) {
45+
/**If this number is the minimum of the unsorted list.*/
46+
int maxIndex = getMaxIndex(numbers, sortedElementsNo);
47+
48+
swap(numbers, sortedElementsNo, maxIndex);
49+
50+
/**DISPLAY ALL STEPS OF THE METHOD*/
51+
showStep(numbers, sortedElementsNo, round);
52+
53+
sortedElementsNo++;
4454
}
45-
System.out.print(numbers[numbers.length - 1]);/**Appending the last element without adding spaces after*/
46-
System.out.println(" ]\n------------------------------------------------------------------------------------------------------------------");
4755
}
4856

4957
static void swap(double[] numbers, int i, int j) {
@@ -62,11 +70,39 @@ static int getMinIndex(double[] numbers, int startPos) {
6270
return min;
6371
}
6472

73+
static int getMaxIndex(double[] numbers, int startPos) {
74+
int max = startPos;
75+
76+
for (int i = startPos + 1; i < numbers.length; i++)
77+
if (numbers[i] > numbers[max]) max = i;
78+
return max;
79+
}
80+
81+
static void showStep(double[] numbers, int sortedElNo, int round) {
82+
83+
System.out.print("round" + round + "=>( ");/**OPEN SORTED SPACE*/
84+
if (sortedElNo < 0) {
85+
System.out.print(" ) [ ");
86+
for (int j = 0; j < numbers.length - 1; j++)
87+
System.out.print(numbers[j] + " ");
88+
} else {
89+
for (int j = 0; j < numbers.length - 1; j++) {
90+
if (j == sortedElNo)
91+
System.out.print(numbers[j] + " ) [ ");/**CLOSE SORTED SPACE*/
92+
else
93+
System.out.print(numbers[j] + " ");
94+
}
95+
}
96+
System.out.print(numbers[numbers.length - 1]);/**Appending the last element without adding spaces after*/
97+
System.out.println(" ]\n------------------------------------------------------------------------------------------------------------------");
98+
}
99+
65100
public static void main(String[] args) {
66101

67102
double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0};
68103

69-
sort(numbers);
104+
sortAscendingOrder(numbers);
105+
sortDescendingOrder(numbers);
70106

71107
}
72108

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /