C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[350] H@_@p Sort
Jisang Yoo [newjisang] 6384 읽음    2002-06-26 02:22
Heap sort 알고리즘을 이용하여 정수배열을 순서대로 정렬시키는 함수를 만든 예제입니다.


/////////////////////////////////////////////////
// 
//   Author: Jisang Yoo
//
//   H@_@p sort
//
/////////////////////////////////////////////////

/*Sort the array 'data[]'*/
void SortIntArray(int a[],int n);

/*Fix the Heap 'heap[]' (In other words, down_heap the heap 'heap[]').
'hn'is the size of the heap.('hn'is the size of 'heap[]' as a heap not as an array.
heap[1]is the first element of the heap. and heap[n]is the last element.
Imagin the array of BASIC.)
't'is the node to fix(or to down_heap).
This function does not change the value of heap[0].(It is for convenience)*/
void FixIntHeap(int heap[], int hn,int t);

void FixIntHeap(int heap[], int hn,int t)
{
  int i,temp=heap[t];
  while(i=2*t,i<=hn)/*while 't'is an internal node*/
  {
    if(i+1<=hn && heap[i]<heap[i+1])/*if 't' have two son and her right son is the bigger.*/
      i=i+1;
    if(heap[i]>temp)/*if the son is bigger than the mother 't'*/
    {
      heap[t]=heap[i];
      t=i;
    }
    else
      break;
  }
  heap[t]=temp;
}

void SortIntArray(int a[],int n)
{
  int i,temp;

  /*Construct heap of size n-1 */
  for(i=(n-1)/2 ;i>=1; i--)
    FixIntHeap(a,n-1,i);

  /*Consume heap up*/
  for(i=n-1;i>=2;i--)
  {
    temp=a[i];
    a[i]=a[1];
    a[1]=temp;
    FixIntHeap(a,i-1,1);
  }

  /*The sorted range is from 1 to n-1.By placing a[0] to a proper place,
  We complete the sorting (ranging from 0 to n-1)*/
  temp=a[0];
  i=0;
  while(a[i+1]<temp && i+1<n)
  {
    a[i]=a[i+1];
    i++;
  }

  a[i]=temp;
}
김백일.cedar [cedar]   2002-06-26 13:16 X
제목을 보고 이게 뭔가하고 한참 생각했습니다. heap sort를 말하는 거군요.
김백일.cedar [cedar]   2002-06-26 13:18 X
에 STL의 heap 연산 알고리듬들에 대해서 올렸습니다. 참고하세요.
김윤동.제라툴 [zeratul]   2002-07-04 09:03 X
자료구조 시간에 잼있게 하던거군요..

+ -

관련 글 리스트
350 H@_@p Sort Jisang Yoo 6384 2002/06/26
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.