Array Sort



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  /******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Heap sort based on CLRS's pseudocode with removed recursion

void heapify(int *A,int i,int heapSize)
{
    int l,r,largest,save;
    int temp;
    do
    {
        l = 2 * i + 1;
        r = 2 * i + 2;
        if(l <= heapSize && A[l] > A[i])
            largest = l;
        else
            largest = i;
        if(r <= heapSize && A[r] > A[largest])
            largest = r;
        save = i;
        if(largest != i)
        {
            temp = A[i];
            A[i] = A[largest];
            A[largest] = temp;
            i = largest;
        }
    }
    while(largest != save);
}

void heapSort(int *A,int n)
{
    int i,heapSize;
    int temp;
    for(i = n/2 - 1;i >= 0;i--)
        heapify(A,i,n-1);
    heapSize = n - 1;
    while(heapSize >= 1)
    {
        temp = A[0];
        A[0] = A[heapSize];
        A[heapSize] = temp;
        heapSize--;
        heapify(A,0,heapSize);
    }
}

// Quick sort based on example from Wirth's Algorithms + Data Structures = Programs

void quickSort(int *A,int l,int r)
{
    int i,j;
    int x,w;
    i = l;
    j = r;
    x = A[(l + r)/2];
    do
    {
        while(A[i] < x) i++;
        while(x < A[j]) j--;
        if(i <= j)
        {
            w = A[i];
            A[i] = A[j];
            A[j] = w;
            i++;
            j--;
        }
    }
    while(i <= j);
    if(l < j) quickSort(A,l,j);
    if(i < r) quickSort(A,i,r);
}

// Shell Sort based on insertion Sort

void ShellSort(int *A,int n)
{
    int i,j,h;
    int buffer;
    h = 1;
    while(h <= n)
    {
        h *= 3;
        h++;
    }
    while(h > 0)
    {
        for(i = h;i < n;i++)
        {
            j = i;
            buffer = A[j];
            while((j >= h) && (A[j-h] > buffer))
            {
                A[j] = A[j - h];
                j -= h;
            }
            A[j] = buffer;
        }
        h /= 3;
    }
}

void printArray(int *A,int n)
{
    int k;
    for(k = 0;k < n;k++)
      printf("%d ",A[k]);
    printf("\n\n");
}

int main()
{
    int k,m,n;
    int *A;
    srand(time(NULL));
    printf("Podaj liczbê elementów tablicy \n");
    scanf("%d",&n);
    printf("Podaj maksymalny zakres przedzia³u dla losowanych liczb \n");
    scanf("%d",&m);
    A = (int *)malloc(n*sizeof(int));
    for(k = 0;k < n;k++)
       A[k] = rand()%m;
    printArray(A,n);
    heapSort(A,n);
    //quickSort(A,0,n-1);
    //ShellSort(A,n);
    printArray(A,n);
    free(A);
    system("PAUSE");
    return 0;
}


How can I eliminate worst case of quicksort using these three algorithms
stop doing recursion of quick sort at some N and just shellsort the list from there. I forget the theoretical N size, but its substantial.
also get a better list of values for your shellsort's H array.

quicksort's recursive calls cost, and shell approaches O(N) as the list approaches sorted. So what happens is that after a few calls, quicksort partly orders the list making shell more efficient, and at some point it costs more to sub-divide than to finish out with shell.
Last edited on
I thougth about switching to heap sort
and using Shell sort as a cut off for small sub array

https://oeis.org/A003586

Here is sequence for Shell sort
Hai He & Gilbert Traub
gave nice way to generate this sequence

Wikipedia claims that complexity of Shell sort with sequence
https://oeis.org/A003586
is O(nlog^2 n)

Last edited on
what exactly do you want to accomplish?
heap is good -- if your stated goal of avoiding worst case is the priority, yes try it.

Ill trust you on the sequence. I was admittedly just looking at it quickly and historically the easy to generate ones have not been ideal.

Topic archived. No new replies allowed.