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 7631 D_C 293
Contributor
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 quicksort(containerIterator begin, containerIterator end){
        if (end > begin){
                containerIterator pivot(begin);
                containerIterator newPivot(partition(begin, end, pivot));
|
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 to programming in Lisp/Scheme,
where I would just pass a lambda. Is a function object something like that
in C++? And in that case, how would I pass the function/operator < (lesser
then).
//quicksort
template<typename BidirectionalIterator>...
|
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 procedure. I got javascript quicksort
running, it's quick but it gets stack overflow after about 5000
elements.
Am I passing the array as a parameter alright? I think javascript
default is to pass pointers, that can be global.
|
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 modifications should I
make?
procedure QuickSort(L, low, high) recursive
Input: L (a list of size n), low, high (integers)
Output: L is sorted
if high > low then
| |
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 executing the OS tell me "quicksort.exe has encountered
a problem and needs to close". When there is no zeros in the integer
array, this doesn't happen.
The pivot is always the last element. Try to change the last element in
the array for a non...
|
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 QUICKSORT with
REGULAR SAMPLING. The idea on the planning is right. the code that I
have written syntactically does not only have problems that it does
not want any to know to work... Are two days that I analyze it but I do
not succeed to find the...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |