473,785 Members | 3,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quicksort

2 New Member
Sorry that this is such a trivial question...

For an assignment I had to write the quicksort program:
Expand|Select|Wrap|Line Numbers
  1.  //This program sorts a set of integers using the quicksort method.
  2.  
  3.  
  4. #include "stdafx.h"
  5. #include "genlib.h"
  6. #include "simpio.h"
  7. #include <stdio.h>
  8.  
  9. #define size 7
  10.  
  11. void getArray(int arr[size]);
  12. void quicksort(int arr[size], int beg, int end);
  13. int partition(int arr[size], int beg, int end);
  14. void dispArray(int arr[size]);
  15. void swap(int arr[], int i, int j);
  16.  
  17. main()
  18. {
  19.     int arr[size], beg, end;
  20.  
  21.     getArray(arr);
  22.     end = size - 1;
  23.     beg = 0;
  24.     quicksort(arr, beg, end);
  25.  
  26.  
  27. }
  28.  
  29. void getArray(int arr[])
  30. {
  31.     int i;
  32.     for (i=0; i <= size - 1; i++)
  33.         {
  34.             printf("\nEnter next integer: ");
  35.             arr[i]=GetInteger();
  36.         }
  37.     dispArray(arr);
  38. }
  39. void dispArray(int arr[]){
  40.  
  41.     int i;
  42.  
  43.     printf("\n");
  44.  
  45.     for (i=0; i <= size - 1; i++)
  46.             {
  47.                 printf("%d ", arr[i]);
  48.             }
  49.             printf("\n");
  50.  
  51. }
  52. void quicksort(int arr[], int beg, int end)
  53. {
  54.  
  55.      int middle;
  56.      if(beg >= end) dispArray(arr);
  57.      else{
  58.       middle = partition(arr, beg, end);
  59.       quicksort(arr, beg, middle - 1);   
  60.       quicksort(arr, middle + 1, end);   
  61.      }
  62.  
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. int partition(int arr[], int beg, int end) {
  70.  
  71. int j;
  72. int pivot;
  73. int i;
  74.  
  75. pivot = beg;
  76. j = beg;
  77.  
  78. for(i=beg+1;i<=end;i++)
  79. {
  80. if (arr[i] < arr[pivot])
  81. {
  82. j++;
  83. swap(arr, j, i);
  84. }
  85. }
  86.  
  87. swap(arr, j, beg);
  88. return(j);
  89. }
  90.  
  91. void swap(int arr[], int i, int j) {
  92. int t = arr[i];
  93. arr[i] = arr[j]; 
  94. arr[j] = t;
  95. }
  96.  
  97.  
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.
Sep 28 '06 #1
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:
Expand|Select|Wrap|Line Numbers
  1. int main(/* args */)
  2. {
  3.   array = getArray();
  4.   printArray(&array);
  5.   quicksortArray(&array);
  6.   printArray(&array);
  7.  
  8.   return EXIT_SUCCESS;
  9. }
That way, you input the array, print it, sort it, then print it again.
Sep 28 '06 #2
Sendell
2 New Member
Ahh, perfect. Thanks!
Sep 28 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5233
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
3
2415
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));
3
3579
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>...
36
5457
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.
2
2944
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
2
2165
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...
6
944
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
2
15495
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...
8
6036
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.
0
9643
marktang
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...
0
9480
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,...
0
9947
tracyyun
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...
0
8968
agi2029
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...
1
7494
isladogs
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...
0
6737
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.