Repository navigation
Expand file tree
/
Copy pathRandomArray.cs
More file actions
80 lines (68 loc) · 2.17 KB
/
Copy pathRandomArray.cs
File metadata and controls
80 lines (68 loc) · 2.17 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
class Program {
public static void random_arr(int arr[,]) {
Console.WriteLine("Random Array: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) Math.floor(Math.random() * (500 + 500) - 500 );
Console.WriteLine(arr[i] + ", ");
}
}
public static boolean isSorted(int arr[,]) {
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1])
return false;
}
return true;
}
public static void sortArr(int arr[,])
{
int n = arr.length;
for (int i = 0; i < n; i++)
{
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
Console.WriteLine(arr[i] + ", ");
}
}
public static void main(String[] args) {
int userChoice = 0;
Console.WriteLine("[Is It Sorted?]");
int arr[] = new int[10];
random_arr(arr);
Console.WriteLine();
do
{
Console.WriteLine("\nWhat do you want to do?\n" +
"1) Generate a new random array\n" +
"2) Is the array sorted?\n" +
"3) Sort the array\n" +
"4) Quit\n" +
"\nChoice: ");
userChoice = Convert.ToInt32(Console.ReadLine());
switch (userChoice) {
case 1:
Console.WriteLine();
random_arr(arr) ;
Console.WriteLine();
break;
case 2:
if (!isSorted(arr)) {
Console.WriteLine("\nThe array is not sorted.");
}
else {
Console.WriteLine("\nThe array is sorted.");
}
break;
case 3:
Console.WriteLine("Sorted Array: "); sortArr(arr);
break;
}
}
while(userChoice != 4);
}
}