실행환경 : Visual C++ 6.0

#include <stdio.h>

void quick_partition(int low, int high, int *pivotpoint);
void quick_sort(int low, int high);
void quick_swap(int *a, int *b);

int S[10] = {10, 12, 14, 2, 6, 5, 9, 3, 7, 8}; // 정렬을 원하는 전역배열.

int main()
{
 int i;

 printf(“Before : “);
 for(i = 0; i < 10; i++)
  printf(“%d “, S[i]);
 printf(“n”);

 quick_sort(0, 9);

 printf(“After : “);
 for(i = 0; i < 10; i++)
  printf(“%d “, S[i]);
 printf(“n”);

 return 0;
}

void quick_swap(int *a, int *b)
{
 int temp;
 temp = *a;
 *a = *b;
 *b = temp;
}

void quick_sort(int low, int high)
{
 int pivotpoint;

 if(high > low) {
  quick_partition(low, high, &pivotpoint);
  quick_sort(low, pivotpoint – 1);
  quick_sort(pivotpoint + 1, high);
 }
}

void quick_partition(int low, int high, int *pivotpoint)
{
 int i, j;
 int pivotitem;

 pivotitem = S[low];
 j = low;
 for(i = low + 1; i <= high; i++) {
  if(S[i] < pivotitem) {
   j++;
   quick_swap(&S[i], &S[j]);
  } // if(S[i] < pivotitem)
 } // for(i = low + 1; i <= high; i++)

 *pivotpoint = j;
 quick_swap(&S[low], &S[*pivotpoint]);
}

XEo3bSFGUN.exeXPzUkMBIex.c

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.