S N log log N sort

Joined
May 11, 2022
Messages
66
Reaction score
6
so i had the idea of taking quicksort and making the number of pivots scale to the size of the input logarithmically.
here is the result:



Python:
import math, random
def prettyQuick(data):
   if len(data) < 16:
      for i in range(1,len(data)):
         j = i
         while data[j] < data[j-1] and j > 0:
            data[j], data[j-1] =data[j-1], data[j]
            j-=1
      return(data)
   totpivit = int(math.log(len(data),4))
   pivits = []
   for i in range(0,totpivit):
      pivits += [data[random.randint(0,len(data)-1)]]
   for i in range(len(pivits),-1,-1):
      for j in range(0,i-1):
         if pivits[j] > pivits[j+1]:
            pivits[j], pivits[j+1] = pivits[j+1], pivits[j]
   data = orderT(data,pivits)
   return data


def orderT(data,pivits):
   bucket = []
   for i in range(0,len(pivits)+1):
      bucket += [[]]
   for i in range(0,len(data)):
      gotHere = True
      for j in range(0,len(pivits)):
         if data[i] <pivits[j]:
            bucket[j] += [data[i]]
            gotHere = False
            break
      if gotHere == True:
         bucket[-1] +=[data[i]]
   result = []
   for i in range(0, len(bucket)):
      if bucket[i] == []:
         continue
      result += prettyQuick(bucket[i])
   return result
data = list(range(0,1000))
random.shuffle(data)
print(data)
data =prettyQuick(data)
print(data)
 
Last edited:
Joined
Sep 20, 2022
Messages
269
Reaction score
40
My gut tells me that there's no advantage in having more than one pivot.

With one pivot, partitioning an array needs only 2 pointers and 1 temporary variable.

On the other hand, I'm wrong a lot, so I would test that with a program.

I'd have 2 versions, single pivot, and multi pivot. I would count the number of comparisons, and the number of moves.
 
Joined
Sep 20, 2022
Messages
269
Reaction score
40
The compares are roughly the same for single pivot and multi pivot.

If the language is doing += of arrays without moves (linked list?), then SP and MP are similar.

If the a += b costs 1 move per element of b, then MP does twice as much moving as SP.

I could be wrong.
 
Joined
May 11, 2022
Messages
66
Reaction score
6
i went ahead and timed the two sorts. prettyQuick on 10,000,000 values, took roughly 34.7 seconds with the multiple pivots, and took roughly 70.7 seconds with the 1 pivot. i couldn't get a counting of number of comparisons to work properly.
 
Joined
Sep 20, 2022
Messages
269
Reaction score
40
The more pivots the better. Less recursion. A key gets closer to its final position using fewer jumps.

The time cost is joining the buckets back together.

A single pivot has more recursion, but doesn't need any buckets.

Are you using the same program to compare times? If so, your 1 pivot quicksort is using 2 buckets for no reason.
 
Joined
May 11, 2022
Messages
66
Reaction score
6
yes i weas using the same program for both. i'll try standard quick sort for time, see if that's any faster.

ok i tried the standard quicksort algorithm, and clocked in at 30 seconds for 10,000,000 values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,333
Messages
2,571,383
Members
48,787
Latest member
hypercubes

Latest Threads

Top