Sorry that this is such a trivial question...
For an assignment I had to write the quicksort program: - //This program sorts a set of integers using the quicksort method.
-
-
-
#include "stdafx.h"
-
#include "genlib.h"
-
#include "simpio.h"
-
#include <stdio.h>
-
-
#define size 7
-
-
void getArray(int arr[size]);
-
void quicksort(int arr[size], int beg, int end);
-
int partition(int arr[size], int beg, int end);
-
void dispArray(int arr[size]);
-
void swap(int arr[], int i, int j);
-
-
main()
-
{
-
int arr[size], beg, end;
-
-
getArray(arr);
-
end = size - 1;
-
beg = 0;
-
quicksort(arr, beg, end);
-
-
-
}
-
-
void getArray(int arr[])
-
{
-
int i;
-
for (i=0; i <= size - 1; i++)
-
{
-
printf("\nEnter next integer: ");
-
arr[i]=GetInteger();
-
}
-
dispArray(arr);
-
}
-
void dispArray(int arr[]){
-
-
int i;
-
-
printf("\n");
-
-
for (i=0; i <= size - 1; i++)
-
{
-
printf("%d ", arr[i]);
-
}
-
printf("\n");
-
-
}
-
void quicksort(int arr[], int beg, int end)
-
{
-
-
int middle;
-
if(beg >= end) dispArray(arr);
-
else{
-
middle = partition(arr, beg, end);
-
quicksort(arr, beg, middle - 1);
-
quicksort(arr, middle + 1, end);
-
}
-
-
-
-
}
-
-
-
-
int partition(int arr[], int beg, int end) {
-
-
int j;
-
int pivot;
-
int i;
-
-
pivot = beg;
-
j = beg;
-
-
for(i=beg+1;i<=end;i++)
-
{
-
if (arr[i] < arr[pivot])
-
{
-
j++;
-
swap(arr, j, i);
-
}
-
}
-
-
swap(arr, j, beg);
-
return(j);
-
}
-
-
void swap(int arr[], int i, int j) {
-
int t = arr[i];
-
arr[i] = arr[j];
-
arr[j] = t;
-
}
-
-
However, this prints the array each step of the process. How do I change my code to only display the array once it is sorted?
Thanks.
2 7586
Take all print statements out of any function. Make a separate function to print the array. Then, change main to something like: - int main(/* args */)
-
{
-
array = getArray();
-
printArray(&array);
-
quicksortArray(&array);
-
printArray(&array);
-
-
return EXIT_SUCCESS;
-
}
That way, you input the array, print it, sort it, then print it again.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: why |
last post by:
Hi,
just been curious. i have learn that quicksorting algorithm is more
widely used then heapsort, despite having the same performance
indication of O(n log n)
anyone knows why?
newbie
|
by: Lieven |
last post by:
I want to write a generic quicksort over STL containers. We have to
parallelize this algorithm afterwards. This is what I've got for now:
template<typename containerIterator>
void...
|
by: Lieven |
last post by:
I want to make a quicksort algorithm that can take both a vector and a list.
This is the code I've got. But I also want to give an argument that will be
used for comparing the elements. I am used...
|
by: HERC777 |
last post by:
just paste the below into notepad, call it iqsort.html double click it
and your IE browser will run it. (neat! a universal programming
language we can all run!)
it falls out from the sort...
|
by: nkharan |
last post by:
Hey,
Here is the pseudocode of quicksort. It can be observed that there
are as many as (n-1) unresolved stack calls in the worst case. Now, how
do i reduce this unresolved stack calls? what...
|
by: camotito |
last post by:
Hi, I want to sort an array of pointers, each position of this array
point to a position in an integer array.
There is a strange behavior when the 'pivot' is '0'. I am using Visual
C++, and when...
|
by: Baltazar007 |
last post by:
Does anyone know how to make quicksort for single linked list without
using array?
I know that mergesort is maybe better but I need quicksort!!
thnx
|
by: simo |
last post by:
Hello to all... I am trying to write an algorithm parallel in order to
realize the quicksort. They are to the first crews with C and MPI. The
algorithm that I am trying to realize is PARALLEL...
|
by: aparnakakkar2003 |
last post by:
hello
can any one tell me how i can create program to sort string
list(standard template library) using quicksort.
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |