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

Commit 12d1428

Browse files
committed
update
1 parent 7d64c28 commit 12d1428

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

DataAndAlgoL/Chpt10SortingAlgorithms/Chpt10Notes.org

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,33 @@
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-
22+
23+
** SELECTION SORT
24+
*** 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
25+
Advantages:
26+
Easy to implement
27+
in-place sort (requires no additional storage space)
28+
Disadvantages:
29+
does not scale well O(n^2)
30+
*** Algorithm used as:
31+
1. finding the minimum value in the list
32+
2. swap it with the value in the current position
33+
3. repeat process for all elements until the entire array is sorted
34+
35+
** INSERTION SORT
36+
simple and efficient comparison sort.
37+
** Each iteration removes an element from the input data and inserts it into the correct position in the list being sorted.
38+
** The choice of the element being removed from the input is random and this process is repeated until all elements are sorted.
39+
** ADVANTAGES:
40+
simple implementation
41+
efficient for small data
42+
adaptive: if the input list is presorted [may not be completely] the insertions sort takes O(n+d), where d is the number of inversions.
43+
Pratically more efficient than selection sort and bubble sort, even tho all of them have O(n^2) worst case complexity
44+
stable: maintains relative order of input data if the keys are the same
45+
in-place: it requires only a constant amount O(1) of additional memory space
46+
Online: insertion sort can sort the list as it receives it
47+
** ALGORITHM:
48+
- 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.
49+
- Sorting is typically done in-place. The resulting array after k iterations has the property where the first k+1 entries are sorted
50+
51+
PAGE -> 514
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class InsertionSort {
6+
public static void main(String[] args) {
7+
int[] nums={2,1,6,34,4,7,55,3};
8+
insertionSort(nums);
9+
}
10+
11+
public static void insertionSort(int[] arr){
12+
13+
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]
17+
while(arr[j] > v && j >= 0){
18+
arr[j+1] = arr[j];
19+
j--; // decrement j by 1 to previous position arr[j-1]
20+
}
21+
22+
arr[j+1]=v;// stores the value from current position that was swapped with value of previous position
23+
}
24+
25+
System.out.println(Arrays.toString(arr));
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort {
6+
public static void main(String[] args) {
7+
int[] nums={2,1,6,34,4,7,55,3};
8+
selectionSort(nums);
9+
10+
}
11+
12+
//O(n^2) runtime. 2 nested for loops goes through n^2 inputs
13+
public static void selectionSort(int[] nums){
14+
int min; // Stores minimum value in list
15+
int temp; // holds values for swappage
16+
17+
for(int i=0; i < nums.length-1; i++){
18+
min=i;
19+
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
20+
if(nums[j] < nums[min]){
21+
min=j;
22+
}
23+
}
24+
//Swap elements
25+
temp= nums[min];
26+
nums[min]= nums[i];
27+
nums[i]=temp;
28+
}
29+
30+
System.out.println(Arrays.toString(nums));
31+
}
32+
}

0 commit comments

Comments
 (0)