Click here to Skip to main content
15,911,890 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how can I change the color of status bar Pin
Cedric Moonen6-Jun-04 20:43
Cedric Moonen6-Jun-04 20:43 
QuestionHow to support Drag 'n Drop out of ZIP archive onto an Applications window / Desktop program icon ? Pin
Defenestration6-Jun-04 11:57
Defenestration6-Jun-04 11:57 
AnswerRe: How to support Drag 'n Drop out of ZIP archive onto an Applications window / Desktop program icon ? Pin
Anthony_Yio6-Jun-04 17:10
Anthony_Yio6-Jun-04 17:10 
AnswerRe: How to support Drag 'n Drop out of ZIP archive onto an Applications window / Desktop program icon ? Pin
Michael Dunn6-Jun-04 20:01
sitebuilderMichael Dunn6-Jun-04 20:01 
GeneralPlease Help!!!I'm disperate Pin
Nemok6-Jun-04 10:21
Nemok6-Jun-04 10:21 
GeneralRe: Please Help!!!I'm disperate Pin
FreeLemons6-Jun-04 10:27
FreeLemons6-Jun-04 10:27 
GeneralRe: Please Help!!!I'm disperate Pin
Nemok8-Jun-04 6:06
Nemok8-Jun-04 6:06 
Generalquicksort with random pivot Pin
FreeLemons6-Jun-04 9:40
FreeLemons6-Jun-04 9:40 
umm. I am trying to convert some existing code I found that performed a quicksort on an array using leftmost element as pivot. I want to make the pivot selection random but I have run into logic errors for a long time. It seems to work sometimes now but not all the time so there is still something wrong and I don't know what. If someone could take a look and tell me where i went wrong I would appreaciate it.


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

#define NUM_ITEMS 100

int numbers[NUM_ITEMS];// = {6,24,80,4,19,84,1,10,13,7};

void print(void);
void quicksort(int beg, int end);
void sort(int beg, int end);
void swap(int left, int right);
int pivot(int beg, int end);
void selectsort(int left, int right);

void main(void)
{
int i;

//seed random number generator
srand((unsigned)time( NULL ));

//fill array with random integers
for (i = 0; i < NUM_ITEMS; i++)
numbers[i] = rand();

//perform quick sort on array
quicksort(0, NUM_ITEMS);

print();

exit(1);
}

void quicksort(int beg, int end)
{
sort(beg, end - 1);
}

void sort(int beg, int end)
{
int position;

if (beg > end)
return;

if (beg == end)
return;

position = pivot(beg, end);
sort(beg, position - 1);
sort(position + 1, end);
}

int pivot(int beg, int end)
{
int left = beg, right = end, pivot;

int rand_subscript;

rand_subscript = (int) ((right-left) * rand() / (RAND_MAX + 1)) + left;

pivot = numbers[rand_subscript];

while ((numbers[left] <= pivot) && (left < end))
{
left++;
}

while (numbers[right] > pivot)
{
right--;
}

while (left < right)
{
swap(left, right);

do
{
left++;
}while ((numbers[left] <= pivot) && (left < end));

do
{
right--;
}while (numbers[right] > pivot);

if (right == rand_subscript)
{
rand_subscript = left;
}
}

if ((numbers[right] != pivot) && (numbers[rand_subscript] == pivot))
swap(right, rand_subscript);

print();

return right;
}

void swap(int left, int right)
{
int hold;

hold = numbers[left];
numbers[left] = numbers[right];
numbers[right] = hold;
}

void selectsort(int left, int right)
{
int l = left, r = right;

int ltemp, value, l_inc;

for (l;l<right;l++)
{
l_inc = l + 1;
ltemp = l;
value = numbers[l];

for(l_inc;l_inc<=right;l_inc++)
{
if (numbers[l_inc]<value)
{
ltemp = l_inc;
value = numbers[l_inc];
}

}
numbers[ltemp] = numbers[l];
numbers[l] = value;
}
}

void print(void)
{
int i;

for (i = 0; i < NUM_ITEMS; i++)
{
printf("%i, ", numbers[i]);
}
printf("\n\n");
}
GeneralRe: quicksort with random pivot Pin
David Crow7-Jun-04 2:34
David Crow7-Jun-04 2:34 
GeneralThreads dialog box Pin
LasVegasGuy6-Jun-04 8:48
LasVegasGuy6-Jun-04 8:48 
GeneralOverloading -&gt; and -&gt;* operators Pin
Alton Williams6-Jun-04 7:00
Alton Williams6-Jun-04 7:00 
GeneralRe: Overloading -&gt; and -&gt;* operators Pin
Diddy6-Jun-04 11:48
Diddy6-Jun-04 11:48 
GeneralADO Record Delete issue Pin
brdavid6-Jun-04 2:13
brdavid6-Jun-04 2:13 
GeneralRe: ADO Record Delete issue Pin
Anthony_Yio6-Jun-04 17:21
Anthony_Yio6-Jun-04 17:21 
GeneralRe: ADO Record Delete issue Pin
brdavid7-Jun-04 9:45
brdavid7-Jun-04 9:45 
GeneralCFormView Size control by button click Pin
Sumit Kapoor5-Jun-04 23:54
Sumit Kapoor5-Jun-04 23:54 
GeneralRe: CFormView Size control by button click Pin
valikac6-Jun-04 4:05
valikac6-Jun-04 4:05 
QuestionWhat is wrong with this code Pin
Boniolopez5-Jun-04 23:55
Boniolopez5-Jun-04 23:55 
AnswerRe: What is wrong with this code Pin
valikac6-Jun-04 12:37
valikac6-Jun-04 12:37 
GeneralRe: What is wrong with this code Pin
Boniolopez7-Jun-04 8:29
Boniolopez7-Jun-04 8:29 
Generalskinmagic skinbuilder problem (repost ) Pin
kanetheterrible15-Jun-04 21:54
kanetheterrible15-Jun-04 21:54 
GeneralRe: skinmagic skinbuilder problem (repost ) Pin
Neville Franks5-Jun-04 23:24
Neville Franks5-Jun-04 23:24 
GeneralRe: skinmagic skinbuilder problem (repost ) Pin
Sumit Kapoor6-Jun-04 0:11
Sumit Kapoor6-Jun-04 0:11 
GeneralPassword protecting other apps Pin
Anonymous5-Jun-04 21:34
Anonymous5-Jun-04 21:34 
GeneralRe: Password protecting other apps Pin
kanetheterrible15-Jun-04 21:56
kanetheterrible15-Jun-04 21:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.