-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathArraySorting.java
More file actions
60 lines (52 loc) · 1.39 KB
/
Copy pathArraySorting.java
File metadata and controls
60 lines (52 loc) · 1.39 KB
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
54
55
56
57
58
59
60
/*
* Created by IntelliJ IDEA.
* User: divyanshb
* Date: 13/01/20
* Time: 3:02 PM
*/
package array;
import java.util.Arrays; //Package
public class ArraySorting //Class
{
public static void main(String[] args)
{
int[] numbers = new int[10];
printArray(numbers);
initializeArray(numbers);
printArray(numbers);
bubbleSort(numbers);
printArray(numbers);
}
public static void bubbleSort(int[] arr) //Declaring function bubbleSort
{
int len = arr.length;
for (int row = 0; row < len; row++) //Outer loop
{
for (int col = 1; col < len - row; col++) //Inner loop
{
if (arr[col - 1] > arr[col])
{
int tmp = arr[col - 1];
arr[col - 1] = arr[col];
arr[col] = tmp;
}
}
}
}
private static void swapArrayValues(int row, int newrow, int[] array) //Swaping values to newrow
{
int temp = array[row];
array[row] = array[newrow];
array[newrow] = temp;
}
public static void initializeArray(int[] array)
{
for (int row = 0; row < array.length; row++)
{
array[row] = 10 - row;
}
}
public static void printArray(int[] array) {
System.out.println(Arrays.toString(array));
}
}