forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
98 lines (83 loc) · 3.06 KB
/
Solution.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
87
88
89
90
91
92
93
94
95
96
97
98
/*
Problem: https://www.hackerrank.com/challenges/quicksort2
C# Language Version: 6.0
.Net Framework Version: 4.7
Tool Version : Visual Studio Community 2017
Thoughts :
1. Let the input array be arr with n elements.
2. Consider a pivot and set it to 0th element of the array.
3. Take three lists named sl, el and bl.
4. Iterate through all elements of arr in a loop
4.1 If current element is smaller than pivot then add it to sl list.
4.2 If current element is bigger than pivot then add it to bl list.
4.3 If current element is equal to pivot then add it to el list.
5. If total items in sl is greater than 1 then repeat steps from 1 to 4 for sl.
6. If total items in bl is greater than 1 then repeat steps from 1 to 4 for bl.
7. Merge all three lists in the order sl,el and bl to create a new array.
8. Print all the elements of the new array (separated by space) obtained after merge in step 7 above.
Time Complexity: O(n^2) //worst case scenario - Eventhough there are no nested for loops but there would be n^2 comparisons
//which happens across recursive calls for partitioned arrays.
Space Complexity: O(n) //an additional array of size n is required to stored the output result. We're NOT following in-place
//sorting here which modifies the input array.
*/
using System;
using System.Collections.Generic;
using System.Linq;
class Solution
{
static int[] QuickSort(int[] arr)
{
var pivot = arr[0];
var smallerItems = new List<int>();
var equalItems = new List<int>();
var biggerItems = new List<int>();
var outputArr = new int[arr.Length];
equalItems.Add(arr[0]);
for (var i = 1; i < arr.Length; i++)
{
if (arr[i] < pivot)
smallerItems.Add(arr[i]);
else if (arr[i] > pivot)
{
biggerItems.Add(arr[i]);
}
else
{
equalItems.Add(arr[i]);
}
}
if (smallerItems.Count > 1)
smallerItems = QuickSort(smallerItems.ToArray()).ToList();
if (biggerItems.Count > 1)
biggerItems = QuickSort(biggerItems.ToArray()).ToList();
var j = 0;
foreach (var item in smallerItems)
{
outputArr[j++] = item;
Console.Write(item);
Console.Write(' ');
}
foreach (var item in equalItems)
{
outputArr[j++] = item;
Console.Write(item);
Console.Write(' ');
}
foreach (var item in biggerItems)
{
outputArr[j++] = item;
Console.Write(item);
Console.Write(' ');
}
Console.WriteLine();
return outputArr;
}
static void Main(String[] args)
{
//No need to capture the size of array. We can use array's length property instead.
Console.ReadLine();
var arr_temp = Console.ReadLine().Split(' ');
var arr = Array.ConvertAll(arr_temp, int.Parse);
QuickSort(arr);
}
}