insertionSort
main.cpp
#include <iostream>
#include <iomanip>
#define MAX_ARY_SIZE 15
using namespace std;
void insertionSort(int list[], int last);
int main()
{
int ary[MAX_ARY_SIZE] = {12,123,34,12,3,65,78,45,67,45,78,65,5,454,576};
cout<<"before sort"<< endl;
for(int i=0; i<MAX_ARY_SIZE; i++)
{
cout<< setw(5)<< *(ary+i);
}
cout<<endl;
insertionSort(ary,MAX_ARY_SIZE-1);
cout<<"after sort"<< endl;
for(int i=0; i<MAX_ARY_SIZE; i++)
{
cout<< setw(5)<< *(ary+i);
}
cout<<endl;
}
void insertionSort(int list[], int last)
{
int current =1;
int hold;
int walker;
for(current = 1; current <=last; current++)
{
hold = list[current];
walker = current -1;
for(walker=current-1; walker >=0 && hold<list[walker]; walker--)
{
list[walker +1] = list[walker];
cout << "passs"<<walker<<endl;
for(int i=0; i<MAX_ARY_SIZE; i++)
{
cout<< setw(5)<< *(list+i);
}
cout<<endl;
}
list[walker +1]= hold;
}
}
/*
void insertionSort(int list[], int last)
{
int current =1;
int hold;
int walker;
while(current<=last) // for(current = 1; current <=last; current++)
{
hold = list[current];
walker = current -1;
while(walker>=0 && hold < list[walker])//for(walker=current-1; walker >=0 && hold<list[walker]; walker++)
{
list[walker +1] = list[walker];
walker = walker -1;
}
list[walker +1]= hold;
current = current +1;
}
return;
}
*/
'1. IT Story > Development' 카테고리의 다른 글
배열로 구현한 Stack을 이용한 중위식구하기 (0) | 2012.03.29 |
---|---|
seqSearch (0) | 2012.03.29 |
AddressListing (0) | 2012.03.29 |
링크드리스트ADT를 이용한 학생성적관리 Pro (0) | 2012.03.29 |
Heap Tree (보기좋게출력) (0) | 2012.03.28 |
하노이타워 (0) | 2012.03.28 |
이진찾기 (0) | 2012.03.28 |
피보나치 (0) | 2012.03.28 |