473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Quicksort

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 7586
D_C
293 100+
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
Ahh, perfect. Thanks!
Sep 28 '06 #3

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

Similar topics

5
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
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...
3
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...
36
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...
2
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...
2
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...
6
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
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...
8
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
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...
1
isladogs
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...
0
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...
0
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...
0
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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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

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.