forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.java
38 lines (35 loc) · 889 Bytes
/
bubble_sort.java
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com;
/**
*
* @author zenalarifin
*/
class BubbleSort{
static int[] arr = new int[10];
public static void main(String[] args){
for(int i=0; i<10; i++)
arr[i] = (int)(Math.random()*10);
System.out.println("Diplay all elements: ");
for(int i : arr)
System.out.print(i+" ");
System.out.println("\n");
//Bubble sort
for(int i=arr.length-1; i>1; i--){
for(int j=0; j<i; j++){
if(arr[j]>arr[j+1])
exchangeValues(j, j+1);
}
}
System.out.println("Sorted elements: ");
for(int i : arr)
System.out.print(i+" ");
}
static void exchangeValues(int index1, int index2){
int t = arr[index1];
arr[index1] = arr[index2];
arr[index2] = t;
}
}