forked from Teju-1212/hacktoberfest-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingUsingloops.java
53 lines (41 loc) · 993 Bytes
/
sortingUsingloops.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
51
52
53
package sortingprb;
import java.util.*;
//Java Program to sort an elements
//by bringing Arrays into play
//Main class
public class sortingUsingloops {
// Main driver method
public static void main(String[] args)
{
// int arr[]= {4,3,6,7,1,2};
Scanner sc=new Scanner(System.in);
// Custom input array
int arr[] = new int[100];
int N;
//num of elements
N = sc.nextInt();
for(int k =0 ; k<N;k++) {
arr[k]=sc.nextInt();
}
// Outer loop
for (int i = 0; i < N; i++) {
// Inner nested loop pointing 1 index ahead
for (int j = i + 1; j < N; j++) {
// Checking elements
int temp = 0;
if (arr[j] < arr[i]) {
// Swapping
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
// System.out.print(arr[i] + " ");
}
}
}
System.out.println("Sorted");
for(int h=0; h<N;h++) {
// Printing sorted array elements
System.out.print(arr[h] + " ");
}
}
}