473,406 Members | 2,352 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,406 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 7589
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.