메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C/C++/MFC

C기초 퀵 정렬(QUICK SORT)

2008.03.14 23:54

WhiteAT 조회 수:27213


// win4.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
class quicksort
 { 
 private: 
  int *x; 
  int items; 
 public: 
  quicksort(int); 
  ~quicksort(); 
  void input(int []); 
  void display(); 
  void sort(int,int);
 };
quicksort::quicksort(int n)
 { 
 items=n; 
 x=new int[items];
 }
quicksort::~quicksort()
 { 
 delete [] x;
 }
void quicksort::input(int a[])
 { 
 for(int i=0;i<items;i++) 
  x[i]=a[i];
 }
void quicksort::display()
 { 
 cout<<"\n Sorted elements are :"; 
 for(int i=0;i<items;i++) 
  cout<<setw(5)<<x[i];
 }
void quicksort::sort(int first,int last)
 { 
 int temp; 
 if(first<last) 
  { 
  int pivot=x[first]; 
  int i=first; 
  int j=last; 
  while(i<j) 
   { 
   while(x[i]<=pivot &&i<last) 
    i++; 
   while(x[j]>=pivot && j>first) 
    j--; 
   if(i<j) 
    { 
    temp=x[i]; 
    x[i]=x[j]; 
    x[j]=temp; 
    } 
   } 
  temp=x[first]; 
  x[first]=x[j]; 
  x[j]=temp; 
  sort(first,j-1); 
  sort(j+1,last); 
  }
 }
int main()
 { 
 int a[100]={1,2,3,4,7,9,44,33,22,55,77,88,};         
 int n=12;   
 quicksort obj(n); 
 obj.input(a); 
 obj.sort(0,n-1); 
 obj.display(); 
 return 0;
 }



<결과>
 Sorted elements are :    1    2    3    4    7    9   22   33   44   55   77 88
관련 문서가 검색되었습니다.
  1. [2013/06/20] 5명의 키를 읽어 들여 가장 큰 키와 작은 키를 구하는 프로그램을 작성하시오 by Question (12064) *1
  2. [2011/03/29] C# DataGridView 간단하게 필터 기능 사용하기 by WhiteAT (27985)
  3. [2010/09/17] ListView Sort 정렬하기 by WhiteAT (22346) *1
  4. [2008/03/14] 쉘 정렬(SHELL SORT) by WhiteAT (12234)
  5. [2008/03/14] 힙 정렬(HEAP SORT) by WhiteAT (13244)
  6. [2008/03/14] 버블 정렬(BUBBLE SORT) by WhiteAT (13053)
  7. [2008/03/14] 합병정렬(MERGE SORT) by WhiteAT (12275)