-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbubbleSort.java
More file actions
37 lines (33 loc) · 839 Bytes
/
bubbleSort.java
File metadata and controls
37 lines (33 loc) · 839 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
package bubblesort;
import java.util.Scanner;
public class bubbleSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("Enter number of elements to sort:");
int n = s.nextInt();
System.out.println("Please input "+n+" numbers");
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter number "+(i+1)+": ");
int ntemp = s.nextInt();
nums[i] = ntemp;
}
nums = bubbleSort(nums);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
}
public static int[] bubbleSort(int[] arr){
for(int i =0;i<arr.length;i++){
for(int j=arr.length-1;j>i;j--){
if(arr[j]<arr[j-1]){
int temp= arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
}
return arr;
}
}