void Pop(int table[],int n)
{
for (int i = 0; i < n; i++)
{
cout<<table[i]<<" ";
}
cout<<endl;
}
void Swap(int& a,int& b)
{
int k = a;
a = b;
b = k;
}
void OutPut(int table[],int m)
{
static int ntimes = 0;
ntimes++;
//printf("the %d times/n",ntimes);
for(int i = 0; i < m; i++)
printf("%d ",table[i]);
printf("/n");
}
/*
6,8,7,5,9,2,1,32
jmp = 8 / 2 = 4
4
jmp = 2
6
*/
//希尔排序
int ShellSort(int table[],int n)
{
int jmp = n;
int j = 0;
while ( (jmp = jmp/2) )
{
j = n - jmp;
for (int i = 0; i < j; i++ )
{
if( table[i] > table[i + jmp] )
Swap(table[i],table[i+jmp]);
}
}
return 0;
}
// int shellSort(int table[], int n)
// {
// int jmp = n;
// int j = 0;
// while( (jmp = jmp / 3 + 1 ) > 1)
// {
// int j = n-jmp;
// for( int i = 0; i < j; i++)
// {
//
// if(table[i] > table[i+jmp])
// Swap(table[i],table[i+jmp]);
// }
//
// }
// return 0;
// }
//直接插入排序
/*
6,8,7,5,9,2,1,32
6,
*/
void InsertSort(int table[],int n)
{
for (int i = 1; i < n; i++)
{
for(int j = 0 ; j < i;j++)
{
if(table[j] > table[i])
Swap(table[j],table[i]);
// OutPut(table,n);
}
}
}
//交换排序 (冒泡排序 和快速排序)
//冒泡排序
void BubleSort(int table[], int n)
{
for( int i = n - 1 ; i >= 0 ; i--)
{
for (int j = 0; j < i ; j++)
{
if( table[j] > table[j+1])
Swap(table[j],table[j+1]);
}
}
}
void BubleSort2(int table[] , int n)
{
int i = n-1;
bool bExchange = true;
while ( bExchange && i >= 0)
{
bExchange = false;
for (int j = 0 ; j < i; j++)
{
if( table[j] > table[j+1])
{
bExchange = true;
Swap(table[j],table[j+1]);
}
}
i--;
}
}
//快速排序
void FastSort(int table[],int left,int right)
{
int i = left;
int j = right;
int vot = table[i];
while (i < j )
{
while( (table[j] >= vot) && (i<j))
j--;
if(i != j)
{
table[i] = table[j];
OutPut(table,8);
cout<<vot<<" i="<<i<<" j="<<j<<endl;
i++;
}
while( (table[i] <= vot) && (i<j))
i++;
if(i != j)
{
table[j]=table[i];
OutPut(table,8);
cout<<vot<<" i="<<i<<" j="<<j<<endl;
j--;
}
}
table[i]=vot;
vot = i;
OutPut(table,8);
cout<<vot<<" i="<<i<<" j="<<j<<endl;
cout<<"------------------------/n";
if( left < vot )
{
FastSort(table,left,vot-1);
}
if( vot < right)
{
FastSort(table,vot+1,right);
}
}
//选择排序。 直接选择排序 堆排序
//直接选择排序
void SelectSort(int table[], int n)
{
for ( int i = 0 ; i < n ; i++)
{
int min = table[i];
int k = i;
for (int j = i; j < n; j++)
{
if( table[j] < table[k])
{
//min = table[j];
k = j;
}
}
if( k != i )
Swap(table[k],table[i]);
}
}
void allpailie(int table[], int size,int count)
{
int start ;
if( count == 1)
{
OutPut(table,size);
return ;
}
start = size - count ;
for( int i = start; i < size; i++)
{
Swap(table[start],table[i]);
allpailie(table,size,count-1);
Swap(table[start],table[i]);
}
}
struct student {
int score;
struct student *next;
};
struct student* create_link() {
struct student *head = NULL;
struct student *tail = NULL;
int score;
while( 1 ) {
cin>>score;
//scanf("%d", &score);
printf("input : %d /n",score);
if(score <= 0)break;
struct student *now = (struct student*)malloc(sizeof(struct student));
now->score = score;
now->next = NULL;
if(head == NULL) {
head = tail = now;
} else {
tail->next = now;
tail = now;
}
}
printf("create link success/n");
return head;
}
void display_link(const struct student *link) {
const struct student *p = link;
for(;p != NULL;p = p->next) {
printf("%d /t", p->score);
}
printf("/n");
}
//select sort and switch point instead of switch data
void sort_link(struct student **head) {
struct student *link = *head;
struct student *pre_p1;
struct student *pre_p2;
struct student *min;
struct student *p1;
for(p1=link; p1->next!=NULL; pre_p1=min,p1=min->next) {
min = p1;
struct student *p2;
for(p2=p1; p2->next!=NULL; p2=p2->next) {
if(p2->next->score < min->score) {
pre_p2 = p2;
min = p2->next;
}
}
if(min == p1) {
printf("no need sort/n", p1->score);
continue;
}
//head or not head
if(p1 == *head) {
*head = min;
} else {
pre_p1->next = min;
}
//switch point
struct student *temp = min->next;
if(p1->next == min) { //p1 and min link directly
min->next = p1;
p1->next = temp;
} else { //p1 and min link indirectly
min->next = p1->next;
pre_p2->next = p1;
p1->next = temp;
}
printf("sort:/t", p1->score);
display_link(*head);
}
}
//插入排序
void sort_link3(struct student **head)
{
struct student *link = *head;
struct student *now;
struct student *prev = NULL;
struct student *pnext = NULL;
struct student *min;
struct student *p;
bool bfirst = true;
while( link != NULL)
{
bool bfind = false;
min = link;
for( p = link; p->next != NULL ; p = p->next)
{
if(p->next->score < min->score)
{
prev = p;
min = p->next;
pnext = min->next;
bfind = true;
}
}
//先从链表中除去最小值
if(bfind)
{
prev->next = pnext;
}
else
{
link = link->next;
}
min->next = NULL;
//加到新链表的后面
if(bfirst)
{
*head = min;
now = min;
bfirst = false;
}
else
{
now->next = min;
now = now->next;
}
}
}