forked from atuldev19/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Sort_java
More file actions
35 lines (31 loc) · 719 Bytes
/
Copy pathArray_Sort_java
File metadata and controls
35 lines (31 loc) · 719 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
public class SortArrayExample3
{
public static void main(String[] args)
{
int i;
int array[] = {12, 45, 1, -1, 0, 4, 56, 23, 89, -21, 56, 27};
System.out.print("Array elements before sorting: \n");
for(i = 0; i < array.length; i++)
System.out.println(array[i]);
sortArray(array, array.length);
System.out.print("Array elements after sorting: \n");
for(i = 0; i <array.length; i++)
{
System.out.println(array[i]);
}
}
private static void sortArray(int array[], int n)
{
for (int i = 1; i < n; i++)
{
int j = i;
int a = array[i];
while ((j > 0) && (array[j-1] > a))
{
array[j] = array[j-1];
j--;
}
array[j] = a;
}
}
}