-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySort.java
More file actions
55 lines (47 loc) · 1.33 KB
/
ArraySort.java
File metadata and controls
55 lines (47 loc) · 1.33 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
/*
* Question : Sort the array of any size without using sort function
* Approach:
* 1. Set an achor element
* 2. Compare this element with the rest of the array
* 3. If we find any element smaller than this number,
* 4. We swap it for the smaller number we found
*/
import java.util.Scanner;
class ArraySort
{
public static void sortInputArray(int[] input)
{
int temp = 0;
for(int i=0; i<input.length; i++)
{
for(int j=i+1; j<input.length; j++)
{
if(input[i]>input[j])
{
temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
}
System.out.println("The sorted array is: ");
for(int k=0; k<input.length; k++)
{
System.out.print(input[k]+" ");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n = sc.nextInt();
int[] inputArray = new int[n];
System.out.println("Enter the "+ n + " array elements: ");
for(int i=0; i<n; i++)
{
inputArray[i] = sc.nextInt();
}
sc.close();
sortInputArray(inputArray);
}
}