Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9ba34d7

Browse files
committed
update on sorting algorithms
1 parent d3ba61a commit 9ba34d7

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

DataAndAlgoL/Chpt10SortingAlgorithms/Bubblesort.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ public static void bubbleSort(int[] array){
2020
}
2121

2222
//O(n) Improved bubble sort Best possible complexity
23-
public static void bubbleSort2(int[] arr){
24-
int temp;
25-
int swapped=1; //flag to check whether its needs to be swapped meaning it is not sorted
26-
for(int i= arr.length-1; i>=0 /* && swapped */; i--){
27-
swapped=0;
28-
for(int j=0; j<=arr.length; j++){
29-
if(arr[i] >arr[i+1]){
30-
//swap elements
31-
temp= arr[i];
32-
arr[i]=arr[i+1];
33-
arr[i+1]=temp;
34-
swapped=1;
35-
}
36-
}
37-
}
38-
}
23+
// public static void bubbleSort2(int[] arr){
24+
// int temp;
25+
// int swapped=1; //flag to check whether its needs to be swapped meaning it is not sorted
26+
// for(int i= arr.length-1; i>=0 /* && swapped */; i--){
27+
// swapped=0;
28+
// for(int j=0; j<=arr.length; j++){
29+
// if(arr[i] >arr[i+1]){
30+
// //swap elements
31+
// temp= arr[i];
32+
// arr[i]=arr[i+1];
33+
// arr[i+1]=temp;
34+
// swapped=1;
35+
// }
36+
// }
37+
// }
38+
// }
3939
}

DataAndAlgoL/Chpt10SortingAlgorithms/Chpt10Notes.org

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Bubble sort continues its iterations until no more swaps are needed.
2020
Has high complexity time and is too Simplest
2121
** Only significant advantage of bubble sort is detecting whether input list is sorted or not
22+
complexity = O(n^2)
2223

2324
** SELECTION SORT
2425
*** Works well for small files. Used to sort very large values with very small keys. Because selecyion is made based on keys and swaps are made only when required
@@ -32,7 +33,7 @@
3233
2. swap it with the value in the current position
3334
3. repeat process for all elements until the entire array is sorted
3435

35-
** INSERTION SORT
36+
** INSERTION SORT !! -> WATCH VIDEO ABOUT INSERTION SORT
3637
simple and efficient comparison sort.
3738
** Each iteration removes an element from the input data and inserts it into the correct position in the list being sorted.
3839
** The choice of the element being removed from the input is random and this process is repeated until all elements are sorted.
@@ -48,4 +49,4 @@
4849
- Every repetition of insertion removes an element from the input data, and inserts it into the correct position in the already sorted list until no input elements remain.
4950
- Sorting is typically done in-place. The resulting array after k iterations has the property where the first k+1 entries are sorted
5051

51-
PAGE -> 514
52+
** SHELL SORT (DIMINISHING INCREMENT SORT)

DataAndAlgoL/Chpt10SortingAlgorithms/InsertionSort.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static void main(String[] args) {
1111
public static void insertionSort(int[] arr){
1212

1313
for(int i=1; i<arr.length; i++){
14-
int v=arr[i]; // holds value of key i
15-
int j=i-1; // holds key i
16-
// while previous key is > and v (current key) and j >=1, assign arr[j] with previous value arr[j-1]
14+
int v=arr[i]; // holds value of index i
15+
int j=i-1; // holds index i-1
16+
// while previous index is > and v (current index value) and j >=1, assign arr[j] with previous value arr[j-1]
1717
while(arr[j] > v && j >= 0){
1818
arr[j+1] = arr[j];
1919
j--; // decrement j by 1 to previous position arr[j-1]

DataAndAlgoL/Chpt10SortingAlgorithms/SelectionSort.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public static void main(String[] args) {
1313
public static void selectionSort(int[] nums){
1414
int min; // Stores minimum value in list
1515
int temp; // holds values for swappage
16-
16+
/*
17+
* Called Selection sort because every j iteration selects the minimum value
18+
* to swap with element at i position
19+
*/
1720
for(int i=0; i < nums.length-1; i++){
1821
min=i;
1922
for(int j= i+1; j< nums.length; j++){ //for each iteration, goes through all the array starting at i and compares j to current min

0 commit comments

Comments
 (0)