Skip to content

devoshm/SortUsingArrayPattern

Repository files navigation

LICENSE

SortUsingArrayPattern

An algorithm that figures out and analyses the pattern of an array and then go for the best sorting algorithm possible for the array.

Objective
When it comes to sorting an array, we have many different algorithms to choose from but which algorithm to choose for a particular situation, to get optimal performance, depends on the input data for that situation.
Imagine an algorithm that takes an array as input, analyses it and figures out the best sorting algorithm for it. Yeah! We did the same in this project so that you need not care about choose the right algorithm for optimal performance anymore.

Pattern Description Sort Algorithm
Repeated Array containing many repeated elements. Count Sort
Nearly Sorted Array is mostly in ascending order. Insertion Sort
Nearly Reversed Array is mostly in descending order. Heap Sort
Random Array in random pattern or if the pattern doesn’t match with any of the above. Quick Sort

Technique Used - Random Sampling:
In this method, we give priority to performance over accuracy. However, we were still able to achieve 96.4% success rate with this method. Here, we randomly pick few elements from the array in the increasing order of the index and give scores for each of the four patterns. The pattern with highest score at the end is taken as the array pattern. The number of elements to be sampled is given by the formula log2n sets, each of size log5n.

Array SizeActual PatternPredicted PatternSampled SizeRepeated ScoreReverse ScoreNearly Sorted ScoreRandom ScoreTime Taken
50000RepeatedRepeated11213860517313299259
50000ReverseReverse1120112-114342272
50000Nearly SortedNearly Sorted1120-111212139414
50000RandomRandom11206447724027845
50000RepeatedRepeated11216459527421102568
50000ReverseReverse1120111033830045
50000Nearly SortedNearly Sorted1120111051021657
50000RandomRandom11205754711723754
50000RepeatedRepeated112795259768487678
50000ReverseReverse1120110153957402
50000Nearly SortedNearly Sorted1120-111211111693
50000RandomRandom11205457792218720
200000RepeatedRepeated14413674699714832677
200000ReverseReverse1440144-112772467
200000Nearly SortedNearly Sorted1440-114411833383
200000RandomRandom14406776931575403
5000RepeatedRepeated781224136559144990
5000ReverseReverse78076151162543
5000Nearly SortedNearly Sorted780-17811148080
5000RandomRandom7804136441253978
5000RepeatedRepeated78783542506224173
5000ReverseReverse7807527805662
5000Nearly SortedNearly Sorted7800773756212
5000RandomRandom780393854877971
5000RepeatedRepeated781224433577720736
5000ReverseReverse78073411868174
5000Nearly SortedNearly Sorted780572111114958
5000RandomRandom7803839541255844
1000RepeatedRepeated50722227348849224
1000ReverseReverse50044513773473
1000Nearly SortedNearly Sorted5018841171207794
1000RandomRandom500292031756679
1000RepeatedRepeated50903019325835103
1000ReverseReverse5004639447382
1000Nearly SortedNearly Sorted50064310472574
1000RandomRandom509222736508963
1000RepeatedRepeated50952623317780916
1000ReverseReverse5004815770208
1000Nearly SortedNearly Sorted5002477647982
1000RandomRandom500232635773473
100RepeatedRepeated2138128157350328
100ReverseReverse21013711313028
100Nearly SortedNearly Sorted2102187249582
100RandomRandom21511914431055
100RepeatedRepeated21136812105614911
100ReverseReverse2101827380672
100Nearly SortedNearly Sorted2104166366677
100RandomRandom21512817444584
60RepeatedRepeated1887710125831372
60ReverseRepeated181511610269643
60Nearly SortedNearly Sorted18051211221125
60RandomRandom18061113248183
60RepeatedRepeated189961196804045
60ReverseReverse1851439281306
60Nearly SortedNearly Sorted18041311219259
60RandomReverse180988243985
60RepeatedRepeated186071098722799
60ReverseReverse1801349270576
60Nearly SortedNearly Sorted1802157253782
60RandomRandom18071015452981

Note: The efficiency of this algorithm can be felt only if the array size is huge. For smaller sized arrays, performing sort directly without prediction is much faster. During our analysis, we felt the impact only when the array size is greater than 5000.

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages