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

Commit 7d64c28

Browse files
committed
update
1 parent d97f033 commit 7d64c28

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package DataAndAlgoL.Chpt10SortingAlgorithms;
2+
3+
public class Bubblesort {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
//O(n^2) time complexity -> extremely slow
9+
public static void bubbleSort(int[] array){
10+
for(int i=array.length -1; i>=0; i--){
11+
for(int j=0; j< array.length; j++){
12+
if(array[i] > array[i+1]){
13+
//swap elements
14+
int temp= array[i];
15+
array[i]=array[i+1];
16+
array[i+1]=temp;
17+
}
18+
}
19+
}
20+
}
21+
22+
//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+
}
39+
}

DataAndAlgoL/Chpt10SortingAlgorithms/Chpt10Notes.org

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@
1111
sort algorithms that use main memory exclusively during the sort called internal sorting. This alfgorithm assummes high speed random access to all memory
1212

1313
* External Sort:
14-
sorting algorithms that use external memory, during the sort.
14+
sorting algorithms that use external memory, during the sort.
15+
16+
** BUBBLE SORT
17+
Simplest sorting algorithm.
18+
*** Works by iterating the input array from the 1st element to the last, comparing each pair of elements and swapping them if needed.
19+
Bubble sort continues its iterations until no more swaps are needed.
20+
Has high complexity time and is too Simplest
21+
** Only significant advantage of bubble sort is detecting whether input list is sorted or not
22+

0 commit comments

Comments
 (0)