标签:style class blog code http tar
持续添加中。。。
1 快排
1 void QuickSort(int data[],int left,int right) 2 { 3 4 if(left<right) 5 { 6 7 int i=left,j=right; 8 int temp=data[left];//把第一个数据作为矛 9 10 while(i<j) 11 { 12 //从后找出第一个小于temp的数 13 while(i<j && data[j]>=temp) 14 j--; 15 16 data[i]=data[j]; 17 18 //从前找出第一个大于temp的数 19 while(i<j && data[i]<=temp) 20 i++; 21 data[j]=data[i]; 22 } 23 //i=j时循环停止 24 data[i]=temp; 25 QuickSort(data,left,i-1); 26 QuickSort(data,i+1,right); 27 } 28 }
1.5链表快排
ref:http://blog.csdn.net/leeboy_wang/article/details/7314473
1 typedef struct LNODE 2 { 3 int data; 4 LNODE *next; 5 }SNNODE; 6 7 void QuickSortList(SNNODE * &head,SNNODE * &end) 8 { 9 SNNODE *head1, *head2, *end1, *end2; //记录每次分割后前后两个链表的头尾节点 10 head1 = head2 = end1 = end2 = NULL; 11 12 if( head == NULL ) 13 return; //如果遍历的当前节点为空,返回 14 15 SNNODE *p, *pre1, *pre2; //用于遍历链表将链表中的元素分成大于key和小于key两个部分 16 p = pre1 = pre2 = NULL; 17 18 int key = head->data; 19 p = head->next; 20 head->next = NULL; //将head的值孤立出来 21 22 //从头到尾遍历链表,把小于key的串到一起,大于等于key的串到一起 23 while( p != NULL ) 24 { 25 //小于key的链表 26 if ( p->data < key ){ 27 if( !head1 ) 28 { 29 head1 = p; 30 pre1 = p; 31 } 32 else{ 33 pre1->next = p; 34 pre1 = p; 35 } 36 p = p->next; 37 pre1->next = NULL; 38 } 39 //大于等于key的链表 40 else 41 { 42 if( !head2 ) 43 { 44 head2 = p; 45 pre2 = p; 46 } 47 else 48 { 49 pre2->next = p; 50 pre2 = p; 51 } 52 p = p->next; 53 pre2->next = NULL; 54 } 55 } 56 57 end1 = pre1; 58 end2 = pre2; //产生新链表的首尾节点 59 60 //对左右两个链表进行递归快排 61 QuickSortList(head1, end1); 62 QuickSortList(head2, end2); 63 64 //从递归栈返回的时候,将key节点和左右两个链表连起来 65 //左右链表都存在 66 if( end1 && head2) 67 { 68 end1->next = head; 69 head->next = head2; 70 head = head1; 71 end = end2; 72 } 73 74 //只有左链表 75 else if(end1) 76 { 77 end1->next = head; 78 end = head; 79 head = head1; 80 } 81 82 //只有右链表 83 else if(head2) 84 { 85 head->next = head2; 86 end = end2; 87 } 88 89 } 90 //测试 91 void testQSList() 92 { 93 94 //新建一个链表 95 96 SNNODE *p1,*p2,*head=NULL; 97 p1=p2=new SNNODE(); 98 cin>>p1->data; 99 while(p1->data!=0) 100 { 101 if(head==NULL) 102 { 103 head=p1; 104 p1->next=NULL; 105 } 106 else 107 { 108 109 p2->next=p1; 110 p2=p1; 111 } 112 //为p1新分配空间,p1是最后一个结点 113 p1=new SNNODE(); 114 cin>>p1->data; 115 p1->next=NULL; 116 } 117 QuickSortList(head,p1); 118 119 //打印 120 SNNODE *current=head; 121 while(current!=NULL ) 122 { 123 cout<<current->data<<" "; 124 current=current->next; 125 } 126 127 128 129 }
2 堆排
#define HEAP_SIZE 10 //获得左子结点 int left(int i) { return 2 * i; } //获得右子结点 int right(int i) { return (2 * i + 1); } //最大根结点调整 void Max_Heapify(int A[], int i, int heap_size) { int l = left(i); int r = right(i); int largest; int temp; //找出三个结点中最大的一个 if(l < heap_size && A[l] > A[i]) { largest = l; } else { largest = i; } if(r < heap_size && A[r] > A[largest]) { largest = r; } //将最大的结点设置为根结点 if(largest != i) { temp = A[i]; A[i] = A[largest]; A[largest] = temp; //递归实现子树最大根结点 Max_Heapify(A, largest, heap_size); } } //建立最大堆 void Build_Max_Heap(int A[]) { for(int i = (HEAP_SIZE-1)/2; i >= 0; i--) { //子树最大堆调整 Max_Heapify(A, i, HEAP_SIZE); } } //堆排序 void HeapSort(int A[], int heap_size) { Build_Max_Heap(A); int temp; for(int i = heap_size - 1; i > 0; i--) { //将最大的放在堆尾 temp = A[0]; A[0] = A[i]; A[i] = temp; //最大根结点调整 Max_Heapify(A, 0, i); } } //测试 void testHeapSort() { int data[]={8, 6, 15, 14, 6, 4, 7, 9, 11,30}; HeapSort(data,10); for(int i=0;i<10;i++) { cout<<data[i]<<" "; } }
3 归并排序
4冒泡排序
5插入排序
6基数排序
参考:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/17/2591457.html
标签:style class blog code http tar
原文地址:http://www.cnblogs.com/mainmonkey/p/3783696.html