Skip to content

Commit be3520a

Browse files
committed
Bubble sort code added
0 parents  commit be3520a

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

.gitignore

Whitespace-only changes.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Intro
2+
3+
4+
### Primitive types or data structures in Java
5+
Integer, Float, Boolean, Char

Sorting/Bubble Sort/Bubble.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
public class Bubble
3+
{
4+
//Following function will sort the array in Increasing (ascending) order
5+
void sort(int arr[])
6+
{
7+
int n = arr.length;
8+
9+
for (int i = 0; i < n-1; i++)
10+
{
11+
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
12+
for (int j = 0; j < n-i-1; j++)
13+
{
14+
if (arr[j] > arr[j+1]) //To Sort in decreasing order, change the comparison operator to '<'
15+
{
16+
int temp = arr[j];
17+
arr[j] = arr[j+1];
18+
arr[j+1] = temp;
19+
}
20+
}
21+
}
22+
}
23+
24+
void modified_sort(int arr[])
25+
{
26+
int n = arr.length;
27+
28+
for (int i = 0; i < n-1; i++)
29+
{
30+
boolean flag = false; //Taking a flag variable
31+
32+
// Last i elements are already in place
33+
for (int j = 0; j < n-i-1; j++)
34+
{
35+
if (arr[j] > arr[j+1]) //To Sort in decreasing order, change the comparison operator to '<'
36+
{
37+
int temp = arr[j];
38+
arr[j] = arr[j+1];
39+
arr[j+1] = temp;
40+
41+
flag = true; //Setting the flag, if swapping occurs
42+
}
43+
}
44+
45+
if(!flag) //If not swapped, that means the list has already sorted
46+
{
47+
break;
48+
}
49+
}
50+
}
51+
}

Sorting/Bubble Sort/Main.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Main
2+
{
3+
// Prints the array
4+
private static void printList(int arr[])
5+
{
6+
int n = arr.length;
7+
for (int i=0; i<n; ++i)
8+
System.out.print(arr[i] + " ");
9+
System.out.println();
10+
}
11+
12+
public static void main(String[] args)
13+
{
14+
Bubble bubble = new Bubble();
15+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
16+
bubble.sort(arr);
17+
//bubble.modified_sort(arr);
18+
System.out.println("Sorted array");
19+
printList(arr);
20+
}
21+
22+
}

Sorting/Bubble Sort/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bubble Sort
2+
3+
**Bubble Sort** is an algorithm which is used to sort **N** elements that are given in a memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one and sort them based on their values.
4+
5+
It is called Bubble sort, because *with each iteration the smaller element in the list bubbles up towards the first place*, just like a water bubble rises up to the water surface.
6+
7+
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.
8+
![bubble sort demonstration](bubble-sort.png)

Sorting/Bubble Sort/bubble-sort.png

8.5 KB
Loading

0 commit comments

Comments
 (0)