-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
86 lines (72 loc) · 2.52 KB
/
Program.cs
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
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace dpeilitz_quicksort
{
class Program
{
static void Main(string[] args){
//parse input
string line;
line = Console.ReadLine();
string[] strings = line.Split();
//remove first element from array
strings = strings.Skip(1).ToArray();
//convert to int array
int[] nums = Array.ConvertAll(strings, s => int.Parse(s));
if(nums.Length != 0) {
SortArray(nums);
}
//convert back to original format
string output = String.Join("\n", nums);
Console.WriteLine(output);
}
private static void SortArray(int[] numbers){
QuickSort(numbers, 0, numbers.Length - 1);
// TA BORT numbers.Sort();
}
private static void QuickSort(int[] numbers, int left, int right)
{
// base case: array size <= 1
if (numbers.Length < 100) {
InsertSort(numbers);
return;
}
if (left >= right) {
return;
}
int pivot = Partition(numbers, left, right);
QuickSort(numbers, left, pivot);
QuickSort(numbers, pivot + 1, right);
}
private static int Partition(int[] numbers, int left, int right)
{
// choose the leftmost element as the pivot
int pivot = numbers[(left + right )/ 2 ];
int pivL = left - 1;
int pivR = right + 1;
while(true){
while(numbers[++pivL] < pivot);
while(numbers[--pivR] > pivot);
if(pivL >= pivR){
return pivR;
}
int temp = numbers[pivR];
numbers[pivR] = numbers[pivL];
numbers[pivL] = temp;
}
}
private static void InsertSort(int[] numbers) {
for(int i = 1; i < numbers.Length; i++) {
int key = numbers[i];
int j = i-1;
while(j >= 0 && numbers[j] > key){
numbers[j+1] = numbers[j];
j = j-1;
numbers[j+1] = key;
}
}
}
}
}