Question

Change low , high , and middle to be pointers to array elements rather than integers...

Change low , high , and middle to be pointers to array elements rather than integers representing the array indices.
Change the split function to return a pointer, not an integer.

/* Sorts an array of integers using Quicksort algorithm */
/* Copyright K.N. King -- C programming Ch9.6 */

#include <stdio.h>

#define N 10

void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);

int main(void){

    int a[N], i;

    printf("Enter %d numbers to be sorted: ", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    quicksort(a, 0, N - 1);

    printf("In sorted order: ");
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);

    printf("\n");

    return 0;
}

void quicksort(int a[], int low, int high){

    int middle;

    if (low >= high)
        return;

    middle = split(a, low, high);
    quicksort(a, low, middle - 1);
    quicksort(a, middle + 1, high);
}

int split(int a[], int low, int high){

    int part_element = a[low];

    for(;;){

        while (low < high && part_element <= a[high])
            high--;

        if(low >= high)
            break;

        a[low++] = a[high];

        while (low < high && a[low] <= part_element)
            low++;

        if (low >= high)
            break;

        a[high--] = a[low];
    }

    a[high] = part_element;

    return high;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#define N 10

void quicksort(int a[], int *low, int *high);
int* split(int a[], int *low, int *high);

int main(void){

int a[N], i;

printf("Enter %d numbers to be sorted: ", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
int low=0,high=N-1;
quicksort(a, &low, &high);
  
printf("In sorted order: ");
for (i = 0; i < N; i++)
printf("%d ", a[i]);

printf("\n");

return 0;
}

void quicksort(int a[], int *low, int *high){

int *middle;

if ((*low) >= (*high))
return;
   int l=*low,r=*high;
middle = split(a, &l, &r);
int m1 = (*middle) - 1;
int m2 = (*middle) + 1;
l=*low;
r=*high;
quicksort(a, &l,&m1 );
quicksort(a, &m2, &r);
  
  
  
  
}

int* split(int a[], int *low, int *high){

int part_element = a[*low];

for(;;){

while (*low < *high && part_element <= a[*high])
(*high)--;

if(*low >= *high)
break;

a[(*low)++] = a[*high];

while (*low < *high && a[*low] <= part_element)
(*low)++;

if (*low >= *high)
break;

a[(*high)--] = a[*low];
}

a[*high] = part_element;

return high;
}

Add a comment
Know the answer?
Add Answer to:
Change low , high , and middle to be pointers to array elements rather than integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • I am writing a program that reads ten integers from standard input, and determines if they...

    I am writing a program that reads ten integers from standard input, and determines if they are going up or down or neither. Can I get some help? I have supplied what I have already. #include <stdio.h> #include <string.h> #include <stdlib.h> int isGoingUp(int [a]); int isGoingDown(int [b]); int main(void) { int array[10]; printf("Enter ten integers : "); for (int a = 0; a < 10; a++) scanf("%d", &array[a]); if (isGoingUp(array) == 1) printf("The values are going up\n"); else if (isGoingDown(array)...

  • The goal is to generate some random number and output a sorted list by quicksort. what...

    The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you #include "pch.h" #include <iostream> using namespace std; /* C implementation QuickSort */ #include<stdio.h> void swap(int* a, int* b) {    int t = *a;    *a = *b;    *b = t; } int partition(int arr[], int low, int high) {   ...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • 1) (5 points) The following program is used to display numbers between two intervals include stdio.h...

    1) (5 points) The following program is used to display numbers between two intervals include stdio.h define true 1 define false 0 5 void prime (int low, int high) int i -o, flag-0 ; printf ("Prime numbers between %d and %d are: ", low, high); while (low <high) 10 flag false; 12 13 for (i 0; i <-low/2; ++i) - 15 16 17 if(low % ?--0) flagtrue: break; 19 20 21 if (flagtrue ) 23 24 25 26 27 28...

  • for c CHALLENGE ACTIVITY 9.7.1: Decrement array elements. Write a loop that subtracts 1 from each...

    for c CHALLENGE ACTIVITY 9.7.1: Decrement array elements. Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex lowerScores = {5,0,2,-3) becomes {4.0, 1.0). 1 include <stdio.h> 3 int main(void) 0 const int SCORES_SIZE - 4; int lowerScores [SCORES_SIZE]: int i; for (1 - 0; i < SCORES_SIZE; ++) { scanf("%d", &(lowerScores[i])); /" Your solution goes here / { 15 for (i = 0; i...

  • The following program sorts the array of numbers entered by the user and prints out the...

    The following program sorts the array of numbers entered by the user and prints out the elements in descending order. It has many errors. Debug the code and show the output. #include <stdio.h> #define N 4 void desc(float *a, float *b) float tp; tp=a; a=b; b=tp; int main() int i, j, ii, mark[N] ; for (ii==0;ii<N;ii++) printf("Enter the %d th element of your array: \n",ii); scanf("%d", mark[ii]); printf("You have entered the following array: \n"); for (ii=0,ii<N,ii++) printf("%d ", mark[ii]); printf("\n...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Refer to the following program sample to answer the parts (a-i) below. Keep in mind that low and high are both indices of array A. /1 Assume that A is a sorted array containing N integers // Assume t...

    Refer to the following program sample to answer the parts (a-i) below. Keep in mind that low and high are both indices of array A. /1 Assume that A is a sorted array containing N integers // Assume that x is a variable of type int int low= 0; // Line 1 int high- N; // Line 2 while (low- high) I/ Line 3 m- (lowthigh)/2; // Line 4 (This is integer division) if (A [m]<) I/Line 5 then low-...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT