davidktw
Arch-Supremacy Member
- Joined
- Apr 15, 2010
- Messages
- 13,547
- Reaction score
- 1,300
Well I often hear leetcode challenges among some posts in this forum.
Perhaps it would be an interesting challenge to code a Quicksort implementation using Bash too ?
Other languages would be such a simple feat to implement Quicksort efficiently. Perhaps the challengers here can do just as good using Bash ?
The fun part has not arrived yet and there will be a reason why I have chosen Bash for this purpose.
Lets start simple first. Lets just implement a Quicksort in Bash first and see how far our HWZ Forum software engineers can achieve. (wink wink)
QUESTION
Implement a Quicksort algorithm using Bash where the implementation should use as much of Bash as possible. While there will be no restriction on how the implementation should be as long as it is written using Bash, challengers are free to use other utilities within the Linux ecosystem as long as the main sorting implementation is written purely in Bash. That means you should not using other programming languages or utilities that perform the actual sorting such as
INPUT
PERFORMANCE
Here we are also interested in performance. As every challenger system will differs, we will try to give a gauge to how good your implementation is
I will use
Hence the performance of your implementation will be pegged relative to the execution time of the linux
By running a sample set of test data of 10,000 lines of random numerical input data which you can download from
https://mega.nz/file/3DBEmBgR#ttIXpIkLFq-vtkPe4S_ZhKAOYyX0r2-mqH60Zwc2K6gand running the following command
That would means my implementation would be approximately 740.5x of the linux
Here is another run against a sample set of 100,000 lines of random numerical input data which you can download from
https://mega.nz/file/mGhHxSST#Yqc3j_zVcwXsa597TnYqHSfEUXoWp9kLlz6BTPbZx-Y
When you use the 100k lines data set on the linux sort utility as such
and upon your own Bash Quicksort implementation, you will get another relative performance.
Mine would be 4660.68x times of linux
As you can see Bash is not exactly performant and as the input size get larger, it is just going to get ridiculously slower.
In case you are wondering how can Bash be so slow ?
These are the timings for the same algorithm written in Perl and ran against the same 2 test cases above.
The timings versus the linux
Perl is already slower than C, but given Perl is a JIT compiled programming language, it is much faster than Bash, which is an interpreted language, in comparison.
However what we are pursing here is creativity when working with constrained scenario and Bash have its quirks, hence it’s up to you to squeeze as much out of it as possible.
We have not hit the limits yet, lets how the basic quicksort implementation progress (^.^)
Have fun coding and hopefully we still have software engineers that remember how to implement Quicksort instead of just knowing how to use.
Invitation to @peterchan75 @ng min teck @Trader11 @diminishin @project_00 and others!
Small hint: Remember how the input may vastly affect the performance of Quicksort ? What makes the worse case complexity of Quicksort ? Take note in your implementation.

Perhaps it would be an interesting challenge to code a Quicksort implementation using Bash too ?
Other languages would be such a simple feat to implement Quicksort efficiently. Perhaps the challengers here can do just as good using Bash ?
The fun part has not arrived yet and there will be a reason why I have chosen Bash for this purpose.
Lets start simple first. Lets just implement a Quicksort in Bash first and see how far our HWZ Forum software engineers can achieve. (wink wink)
QUESTION
Implement a Quicksort algorithm using Bash where the implementation should use as much of Bash as possible. While there will be no restriction on how the implementation should be as long as it is written using Bash, challengers are free to use other utilities within the Linux ecosystem as long as the main sorting implementation is written purely in Bash. That means you should not using other programming languages or utilities that perform the actual sorting such as
sort, or other programming tools like awk, perl, python etc.INPUT
0 - Nnumeric values delimited by newline characters will be provided from standard IN stream.- Each numerical value can be in the integral range
[-2^63,(2^63)-1] - Input is only limited to numerical values, string comparison is not required.
0 <= N <= 100000
- The sorted
0 - Nnumeric values, by ascending order (top down) will be printed to the standard OUT stream delimited by newline characters - Nothing should be printed if
N == 0
Code:
$ echo -ne '' | ./quicksort.sh
$ echo '' | ./quicksort.sh
$ echo '1' | ./quicksort.sh
1
$ seq 10 1 | ./quicksort.sh
1
2
3
4
5
6
7
8
9
10
$ (seq 10 1;seq 0 -10) | ./quicksort.sh
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
PERFORMANCE
Here we are also interested in performance. As every challenger system will differs, we will try to give a gauge to how good your implementation is
I will use
sort as a measure which we can run on our system
Code:
$ time sort -n <quicksort.sample.test.data.10k.txt >/dev/null
real 0m0.008s
user 0m0.008s
sys 0m0.001s
Hence the performance of your implementation will be pegged relative to the execution time of the linux
sort tool using the exact command line arguments as shown above.By running a sample set of test data of 10,000 lines of random numerical input data which you can download from
https://mega.nz/file/3DBEmBgR#ttIXpIkLFq-vtkPe4S_ZhKAOYyX0r2-mqH60Zwc2K6gand running the following command
Code:
$ time ./quicksort.sh <quicksort.sample.test.data.10k.txt >/dev/null
real 0m5.924s
user 0m5.915s
sys 0m0.008s
That would means my implementation would be approximately 740.5x of the linux
sort execution time. The lower this relative value is, the better your implementation is.Here is another run against a sample set of 100,000 lines of random numerical input data which you can download from
https://mega.nz/file/mGhHxSST#Yqc3j_zVcwXsa597TnYqHSfEUXoWp9kLlz6BTPbZx-Y
When you use the 100k lines data set on the linux sort utility as such
Code:
$ time sort -n <quicksort.sample.test.data.100k.txt >/dev/null
real 0m0.072s
user 0m0.052s
sys 0m0.020s
and upon your own Bash Quicksort implementation, you will get another relative performance.
Code:
$ time ./quicksort.sh <quicksort.sample.test.data.100k.txt >/dev/null
real 5m35.569s
user 5m35.444s
sys 0m0.080s
Mine would be 4660.68x times of linux
sort utility timingAs you can see Bash is not exactly performant and as the input size get larger, it is just going to get ridiculously slower.
In case you are wondering how can Bash be so slow ?
These are the timings for the same algorithm written in Perl and ran against the same 2 test cases above.
The timings versus the linux
sort utility are relatively 8.0x and 6.3x respectively
Code:
$ time <quicksort.sample.test.data.10k.txt ./quicksort.pl >/dev/null
real 0m0.064s
user 0m0.060s
sys 0m0.005s
$ time <quicksort.sample.test.data.100k.txt ./quicksort.pl >/dev/null
real 0m0.454s
user 0m0.446s
sys 0m0.008s
Perl is already slower than C, but given Perl is a JIT compiled programming language, it is much faster than Bash, which is an interpreted language, in comparison.
However what we are pursing here is creativity when working with constrained scenario and Bash have its quirks, hence it’s up to you to squeeze as much out of it as possible.
We have not hit the limits yet, lets how the basic quicksort implementation progress (^.^)
Have fun coding and hopefully we still have software engineers that remember how to implement Quicksort instead of just knowing how to use.
Invitation to @peterchan75 @ng min teck @Trader11 @diminishin @project_00 and others!
Small hint: Remember how the input may vastly affect the performance of Quicksort ? What makes the worse case complexity of Quicksort ? Take note in your implementation.
Last edited: