-
Notifications
You must be signed in to change notification settings - Fork 693
/
Solution.cs
74 lines (63 loc) · 2.35 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
/*
Problem: https://www.hackerrank.com/challenges/quicksort1/problem
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 ll, el and gl.
4. Iterate through all elements of arr in a loop
4.1 If current element is less than pivot then add it to ll list.
4.2 If current element is greater than pivot then add it to gl list.
4.3 If current element is equal to pivot then add it to el list.
5. Merge all three lists in the order ll,el and gl to create a new array.
6. Print all the elements of the new array (separated by space) obtained after merge in step 5 above.
Time Complexity: O(n) //There are no nested loops
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;
class Solution
{
static int[] QuickSort(int[] arr)
{
var pivot = arr[0];
var lessList = new List<int>();
var equalList = new List<int>();
var moreList = new List<int>();
var outputArr = new int[arr.Length];
equalList.Add(arr[0]);
for (var i = 1; i < arr.Length; i++)
{
if (arr[i] < pivot)
lessList.Add(arr[i]);
else if (arr[i] > pivot)
{
moreList.Add(arr[i]);
}
else
{
equalList.Add(arr[i]);
}
}
var j = 0;
foreach (var item in lessList)
outputArr[j++] = item;
foreach (var item in equalList)
outputArr[j++] = item;
foreach (var item in moreList)
outputArr[j++] = item;
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);
var result = QuickSort(arr);
Console.WriteLine(string.Join(" ", result));
}
}