排序代码实现

/*
 快速排序
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=100;

int s[MAXN];


int AdjustArray(int s[],int l,int r)
{
    int i=l,j=r;
    int x=s[l];
    while(i<j)
    {
        while( i<j && s[j]>=x)
            j--;
        if(i<j)
            s[i++]=s[j];
        while( i<j && s[i]<x  )
            i++;
        if(i<j)
            s[j--]=s[i];
    }
    s[i]=x;
    return i;
}

void Quick_Sort(int s[],int l,int r)
{
    if(l<r)
    {
       int i=AdjustArray(s,l,r);
       Quick_Sort(s,l,i-1);
       Quick_Sort(s,i+1,r);
    }
}

int main()
{
    int n;
    while(cin>>n)
    {
       if(n>=MAXN)
       {
         printf("缓冲区溢出,");
         exit(0);
       }
       for(int i=1;i<=n;i++)cin>>s[i];

    }
    return 0;
}

/*
   归并排序
*/

//将有二个有序数列a[first...mid]和a[mid...last]合并。  
void mergearray(int a[], int first, int mid, int last, int temp[])  
{  
    int i = first, j = mid + 1;  
    int m = mid,   n = last;  
    int k = 0;  
      
    while (i <= m && j <= n)  
    {  
        if (a[i] <= a[j])  
            temp[k++] = a[i++];  
        else  
            temp[k++] = a[j++];  
    }  
      
    while (i <= m)  
        temp[k++] = a[i++];  
      
    while (j <= n)  
        temp[k++] = a[j++];  
      
    for (i = 0; i < k; i++)  
        a[first + i] = temp[i];  
}  
void mergesort(int a[], int first, int last, int temp[])  
{  
    if (first < last)  
    {  
        int mid = (first + last) / 2;  
        mergesort(a, first, mid, temp);    //左边有序  
        mergesort(a, mid + 1, last, temp); //右边有序  
        mergearray(a, first, mid, last, temp); //再将二个有序数列合并  
    }  
}  
  
bool MergeSort(int a[], int n)  
{  
    int *p = new int[n];  
    if (p == NULL)  
        return false;  
    mergesort(a, 0, n - 1, p);  
    delete[] p;  
    return true;  
}  

/*
  堆排序
*/
/*堆排序(大顶堆)*/ 

#include <iostream>
#include<algorithm>
using namespace std;

void HeapAdjust(int *a,int i,int size)  //调整堆 
{
    int lchild=2*i;       //i的左孩子节点序号 
    int rchild=2*i+1;     //i的右孩子节点序号 
    int max=i;            //临时变量 
    if(i<=size/2)          //如果i是叶节点就不用进行调整 
    {
        if(lchild<=size&&a[lchild]>a[max])
        {
            max=lchild;
        }    
        if(rchild<=size&&a[rchild]>a[max])
        {
            max=rchild;
        }
        if(max!=i)
        {
            swap(a[i],a[max]);
            HeapAdjust(a,max,size);    //避免调整之后以max为父节点的子树不是堆 
        }
    }        
}

void BuildHeap(int *a,int size)    //建立堆 
{
    int i;
    for(i=size/2;i>=1;i--)    //非叶节点最大序号值为size/2 
    {
        HeapAdjust(a,i,size);    
    }    
} 

void HeapSort(int *a,int size)    //堆排序 
{
    int i;
    BuildHeap(a,size);
    for(i=size;i>=1;i--)
    {
        //cout<<a[1]<<" ";
        swap(a[1],a[i]);           //交换堆顶和最后一个元素,即每次将剩余元素中的最大者放到最后面 
          //BuildHeap(a,i-1);        //将余下元素重新建立为大顶堆 
          HeapAdjust(a,1,i-1);      //重新调整堆顶节点成为大顶堆
    }
} 

int main(int argc, char *argv[])
{
     //int a[]={0,16,20,3,11,17,8};
    int a[100];
    int size;
    while(scanf("%d",&size)==1&&size>0)
    {
        int i;
        for(i=1;i<=size;i++)
            cin>>a[i];
        HeapSort(a,size);
        for(i=1;i<=size;i++)
            cout<<a[i]<<"";
        cout<<endl;
    }
    return 0;
}<pre name="code" class="cpp">#include <stdio.h>
#include <stdlib.h>
//节点数据结构
struct node{
int val;//值
int bf;//平衡因子
struct node* left;
struct node* right;
};
 
struct node* root;
//struct node* top;
//获得父节点的函数
struct node* GetFather(struct node* son){
	struct node* temp;
	if(son==root)
		return NULL;
	temp=root;
	while(1){
		if(temp==NULL)
			return NULL;
		else if(son->val<=temp->val){
			if(temp->left == son)
				return temp;
			else{
				temp = temp->left;
				continue;
			}
		}
		else if(son->val>temp->val){
			if(temp->right == son)
				return temp;
			else{
				temp = temp->right;
				continue;
			}
		}
	}
}
 
//LL类型旋转
int RorateLL(struct node* inode, struct node* blance){
	struct node* father;
	struct node* left;
	left = blance->left;
	if(father=GetFather(blance)){
		if(blance->val<=father->val)
			father->left = left;
		else 
			father->right = left;
	}
	else
		root = left;
	left->bf = 0;
	blance->bf = 0;
	blance->left = left->right;
	left->right=blance;
	father = left->left;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
}
//RR类型旋转
int RorateRR(struct node* inode, struct node* blance){
	struct node* father;
	struct node* right;
	right = blance->right;
	if(father=GetFather(blance)){
		if(blance->val<=father->val)
			father->left = right;
		else 
			father->right = right;
	}
	else 
		root = right;
	right->bf = 0;
	blance->bf = 0;
	blance->right = right->left;
	right->left=blance;
	father = right->right;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
}
//LR类型旋转
int RorateLR(struct node* inode, struct node* blance){
	struct node* father;
	struct node* newtop;
	father = blance;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
 
	newtop = blance->left->right;
	if(father=GetFather(blance)){
 
		if(blance->val<=father->val)
			father->left = newtop;
		else 
			father->right = newtop;
	}
	else 
		root = newtop;
	blance->left->right = newtop->left;
	newtop->left = blance->left;
	blance->left = newtop->right;
	newtop->right=blance;
	if(newtop->bf == 0){
		newtop->left->bf==0;
		newtop->right->bf==0;
	}
	else if(newtop->bf == 1){
		newtop->left->bf==0;
		newtop->right->bf==-1;
	}
	else if(newtop->bf == -1){
		newtop->left->bf==1;
		newtop->right->bf==0;
	}
	return 1;
 
}
//RL类型旋转
int RorateRL(struct node* inode, struct node* blance){
	struct node* father;
	struct node* newtop;
	father = blance;
	while(father!=inode){
		if(inode->val<=father->val){
			father->bf++;
			father = father->left;
		}
		else{
			father->bf--;
			father = father->right;
		}
 
	}
 
	newtop = blance->right->left;
	if(father=GetFather(blance)){
 
		if(blance->val<=father->val)
			father->left = newtop;
		else 
			father->right = newtop;
	}
	else root=newtop;
	blance->right->left = newtop->right;
	newtop->right = blance->right;
	blance->right = newtop->left;
	newtop->left=blance;
	if(newtop->bf == 0){
		newtop->left->bf==0;
		newtop->right->bf==0;
	}
	else if(newtop->bf == 1){
		newtop->right->bf==0;
		newtop->left->bf==-1;
	}
	else if(newtop->bf == -1){
		newtop->right->bf==1;
		newtop->left->bf==0;
	}
	return 1;
 
}
//插入函数
int AvlInsert(struct node* inode){
	struct node* blance;
	struct node* temp;
	temp=root;
	while(1){
		if(temp->bf==1|temp->bf==-1)
			blance = temp;
		if(inode->val<=temp->val){
			if(temp->left){
				temp=temp->left;
				continue;
			}
			else{
				temp->left=inode;
				break;
			}
		}
		else if(inode->val>temp->val){
			if(temp->right){
				temp=temp->right;
				continue;
			}
			else{
				temp->right=inode;
				break;
			}
		}
	}
 
	if(!blance){
		temp=root;
		while(temp!=inode){
			if(inode->val<=temp->val){
				temp->bf++;
				temp=temp->left;
			}
			else{
				temp->bf--;
				temp=temp->right;
			}
 
		}
		return 1;
 
	}
	else{
		if(blance->bf==-1 && blance->val>=inode->val || blance->bf==1 && blance->val<inode->val){
			blance->bf=0;
			return 1;
		}
		else if(inode->val<=blance->val && inode->val<=blance->left->val)
			RorateLL(inode, blance);
		else if(inode->val<=blance->val && inode->val>blance->left->val)
			RorateLR(inode, blance);
		else if(inode->val>blance->val && inode->val<=blance->right->val)
			RorateRL(inode, blance);
		else if(inode->val>blance->val && inode->val>blance->right->val)
			RorateRR(inode, blance);
 
	}	
	return 1;
 
}
 
int AvlCreate(struct node* inode){
 
	if(!root){
		root = inode;
		return 1;
	}	
	AvlInsert(inode);
 
}
 
//打印整个树的函数
void Traveser(struct node* r)
{   
     if(r->left) Traveser(r->left);
     if(r) printf("%d, %d\n",r->val, r->bf);
     if(r->right) Traveser(r->right);   
}
 
int main(){
 
	int a;
	struct node* Node;
	while(1){
		scanf("%d",&a);
		if (a==-1)
			break;
		Node = malloc(sizeof(struct node));
		Node->val=a;
		Node->bf=0;
		Node->left=NULL;
		Node->right=NULL;
		AvlCreate(Node);
	}
	Traveser(root);
 
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值