-
Notifications
You must be signed in to change notification settings - Fork 96
/
ascBubbleSort.java
50 lines (49 loc) · 1.18 KB
/
ascBubbleSort.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
39
40
41
42
43
44
45
46
47
48
49
50
/**
* BUBBLE SORT
* --ASCENDING
* --STABLE SORT
**/
import java.util.*;
public class ascBubbleSort
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); //Scanner for input
int n; //size of the array
System.out.println("Enter the size of the array");
n = input.nextInt();
int[] arr = new int[n]; //declared array
for(int i=0;i<n;i++) //inputing array
{
arr[i] = input.nextInt();
}
input.close();
bubblesort(arr); //BUBBLE SORT
System.out.println("After sorting:"); //output array
for(int k=0;k<arr.length;k++)
{
System.out.print(arr[k]+" ");
}
}
public static void bubblesort(int[] array)
{
int i,j;
for(i=array.length-1;i>0;i--)
{
for(j=0;j<i;j++)
{
if(array[j] > array[j+1])
{
//swapp
swap(array,j,j+1);
}
}
}
}
public static void swap(int[] ary,int a,int b)
{
int temp = ary[a];
ary[a] = ary[b];
ary[b] = temp;
}
}