메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C/C++/MFC

C기초 버블 정렬(BUBBLE SORT)

2008.03.14 23:47

WhiteAT 조회 수:13053


버블 정렬(BUBBLE SORT)

// win4.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
class CWATBubbleSort
 { 
 private: 
  int *x; 
  int items; 
 public: 
  CWATBubbleSort(int); 
  ~CWATBubbleSort(); 
  void input(int []); 
  void display(); 
  void sort();
 };
CWATBubbleSort::CWATBubbleSort(int n)
{ 
 items=n; 
 x=new int[items];
}
CWATBubbleSort::~CWATBubbleSort()
{ 
 delete [] x;
}
void CWATBubbleSort::input(int a[])
{ 
 for(int i=0;i<items;i++) 
  x[i]=a[i];
}
void CWATBubbleSort::sort()
{ 
 for(int i=0;i<items;i++) 
 { 
 for(int j=0;j<items-(i+1);j++) 
  {
  if(x[j]>x[j+1]) 
   {
   int temp; 
   temp=x[j]; 
   x[j]=x[j+1]; 
   x[j+1]=temp; 
   } 
  } 
 }
}
void CWATBubbleSort::display()
{ 
 for(int i=0;i<items;i++) 
  cout<<" "<<x[i];
}
int main()
{ 
 int a[100]={1,2,3,4,7,9,44,33,22,55,77,88,};   
 int n=12;   
 CWATBubbleSort obj(n); 
 obj.input(a); 
 obj.sort(); 
 obj.display(); 
 return 0;
}


<결과>
 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] 퀵 정렬(QUICK SORT) by WhiteAT (27213)
  6. [2008/03/14] 힙 정렬(HEAP SORT) by WhiteAT (13244)
  7. [2008/03/14] 합병정렬(MERGE SORT) by WhiteAT (12275)