C/C++/MFC
C기초 쉘 정렬(SHELL SORT)
2008.03.14 23:57
쉘 정렬(SHELL SORT)
// win4.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다. //
#include "stdafx.h" #include<iostream> #include<iomanip> using namespace std;
class shellsort { private: int *x; int items; public: shellsort(int); ~shellsort(); void input(int [ ]); void display(); void sort(); };
shellsort::shellsort(int n) { items=n; x=new int[items]; }
shellsort::~shellsort() { delete [ ] x; }
void shellsort::input(int a[ ]) { for(int i=0;i<items;i++) x[i]=a[i]; }
void shellsort::display() { cout<<"\n Sorted elements are :"; for(int i=0;i<items;i++) cout<<setw(5)<<x[i]; }
void shellsort::sort() { int temp,i,h; for(h=1;h<items/9;h=3*h+1); for(;h>0;h /=3) { for(i=h;i<items;i++) {
int j; temp=x[i]; for(j=i-h;j>=0;j -=h) { if(temp<x[j]) x[j+h]=x[j]; else break; } x[j+h]=temp; } } }
int main() { int a[100]={1,2,3,4,7,9,44,33,22,55,77,88,}; int n=12;
shellsort obj(n); obj.input(a); obj.sort(); 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] 퀵 정렬(QUICK SORT) ()
- [2008/03/14] 힙 정렬(HEAP SORT) ()
- [2008/03/14] 버블 정렬(BUBBLE SORT) ()
- [2008/03/14] 합병정렬(MERGE SORT) ()