By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,236 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,236 IT Pros & Developers. It's quick & easy.

Quicksort

P: 2
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
Share this Question
Share on Google+
2 Replies


100+
P: 293
D_C
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

P: 2
Ahh, perfect. Thanks!
Sep 28 '06 #3

Post your reply

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