C/C++/MFC
C기초 퀵 정렬(QUICK SORT)
2008.03.14 23:54
// 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
관련 문서가 검색되었습니다.
- [2013/06/20] 5명의 키를 읽어 들여 가장 큰 키와 작은 키를 구하는 프로그램을 작성하시오 (12064) *1
- [2011/03/29] C# DataGridView 간단하게 필터 기능 사용하기 (27985)
- [2010/09/17] ListView Sort 정렬하기 (22346) *1
- [2008/03/14] 쉘 정렬(SHELL SORT) (12234)
- [2008/03/14] 힙 정렬(HEAP SORT) (13244)
- [2008/03/14] 버블 정렬(BUBBLE SORT) (13053)
- [2008/03/14] 합병정렬(MERGE SORT) (12275)
댓글 0
번호 | 제목 |
---|---|
공지 | 몇 가지 TIP 모음 |
공지 | Dialog 형태의 프로그램 만들기 [2] |
공지 | Visual C++ 에 유용한 링크 모음입니다. |
» | 퀵 정렬(QUICK SORT) |