forked from abhishekchandra2522k/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_sort.java
More file actions
50 lines (38 loc) · 904 Bytes
/
Bubble_sort.java
File metadata and controls
50 lines (38 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package searching_and_sorting;
import java.util.Scanner;
public class Bubble_sort {
static Scanner s = new Scanner(System.in);
//function to print array
public static void printArray(int[] arr) {
for (int element : arr) {
System.out.print(element + " ");
}
System.out.println();
}
//end
//bubble sort function
public static void bubbleSort(int[] arr){
for(int i = 0; i< arr.length - 1;i++)
{
for(int j = 0; j< arr.length - 1; j++)
{
if(arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//ends
public static void main(String[] args) {
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
Bubble_sort.bubbleSort(arr);
printArray(arr);
}
}