@@ -5,3 +5,121 @@ order: "5C"
5
5
section : " Searching & Sorting"
6
6
description : " learn Searching n sorting algorithms"
7
7
---
8
+
9
+
10
+ Sorting is an everyday tool for programmers.We need to sort many things for many reasons.Like sorting to age,price etc.So
11
+ there are many sorting algorithms like Merge Sort , Quick Sort, Bubble sort etc.
12
+ ## ** Stable & Unstable Sorting**
13
+ If Two elements in array has same value, After sorting two things can happen.
14
+ 1 . They exchange their relative position.
15
+ 2 . They don't exchange their relative position.
16
+ ![ Stable Sorting] ( ./images/stableSort.jpeg )
17
+
18
+ From this example we can see that two '26' doesn't replace their relative positions.Hence it is
19
+ stable sorting algorithms.
20
+ Merge Sort ,Bubble Sort, insertion sort are actually stable sorting algorithm.
21
+ On the other hand,Heap Sort, Quick Sort etc are unstable sorting algorithms.
22
+
23
+ ## ** Bubble Sort**
24
+ Bubble Sort is the simplest algorithm. This is named Bubble Sort because its element just swap
25
+ one by one.
26
+ It doesn't have any special use in sorting.It is actually used for educational purposes to introduces
27
+ sorting algorithms. I'm actually sorry if I disappointed you. Sort your emotions then.
28
+
29
+
30
+ <ul >
31
+ Suppose we have an array of 3 numbers [ 3,6,2] which should be sorted in incrementing order. We can use Bubble Sort
32
+ Algorithms to do so.
33
+ <li >
34
+ At first, we have to take the first element of the array and compare it to the rest of the
35
+ elements.If we get any elements lower than it, we will swap the position.
36
+ Comparison 1: 3 > 6 ->NO
37
+ </li >
38
+
39
+ <li >
40
+ Comparison 2: 3 > 2
41
+ ->YES swap the positions Then the array will be [ 2,6,3]
42
+
43
+ </li >
44
+ <li >
45
+ Comparison 3: 6>3 -> YES swap the positions Then the array will be [ 2,3,6]
46
+
47
+ </li >
48
+ </ul >
49
+ Boom, We have sorted the array.Now let's try to do this in Java.
50
+
51
+ ## ** Bubble Sort Function**
52
+ ``` java
53
+ void bubbleSort(int array[], int length){
54
+
55
+ for (int i = 0 ; i < length- 1 ; i++ ){
56
+ for (int j = i+ 1 ; j < length- 1 ; j++ ){
57
+ // first loop is used to check to the (length-1)th index of the aray
58
+ // second loop is used to check the following items of the arrays
59
+ int temp;
60
+ if (array[i]< array[j]){
61
+ temp = array[i];
62
+ array[i]= array[j];
63
+ array[j]= temp;
64
+ }
65
+ }
66
+ }
67
+
68
+ ```
69
+
70
+ ## ** Complete Program In Java **
71
+ ```java
72
+ public class Main {
73
+ static int array[] = {2 , 1 , 6 , 5 , 4 };
74
+
75
+
76
+ public static void main (String [] args ) {
77
+
78
+ int length = array. length;
79
+ System . out. println(" Unsorted Array" );
80
+
81
+ for (int i = 0 ; i < length; i++ ) {
82
+ System . out. println(array[i]);
83
+ }
84
+
85
+ System . out. println(" Unsorted Array" );
86
+ bubbleSort(array, length);
87
+
88
+
89
+
90
+
91
+
92
+
93
+ }
94
+
95
+ public static void bubbleSort (int array [], int length ) {
96
+
97
+ for (int i = 0 ; i < length - 1 ; i++ ) {
98
+ for (int j = i + 1 ; j < length ; j++ ) {
99
+ // first loop is used to check to the (length-1)th index of the aray
100
+ // second loop is used to check the following items of the arrays
101
+ int temp;
102
+ if (array[i] > array[j]) {
103
+ temp = array[i];
104
+ array[i] = array[j];
105
+ array[j] = temp;
106
+ }
107
+ }
108
+ }
109
+ // for printing
110
+
111
+ for (int i = 0 ; i < length; i++ ) {
112
+ System . out. println(array[i]);
113
+ }
114
+ }
115
+ }
116
+
117
+ ```
118
+ ## ** Time Complexity **
119
+ Now talk about the time complexity of this algorithm. From the coding algorithm that is clear that we have to use a loop and
120
+ a nested loop. So the complexity becomes O(n* n).
121
+
122
+ ## ** Space Complexity **
123
+ Here Space complexity is O(1 ). We just have to make a temp variable which is updated throughout the loop and doesn' t need to
124
+ create anything new.
125
+
0 commit comments