sort相关
[java] view plaincopy
- /**
- *
- * Heap sort
- * @author ljs
- * 2011-06-03
- *
- *
- */
- public class HeapSort {
- public static void solve(int[] data){
- heapify(data);
- int len = data.length;
- //sort: remove the top element and put it at the right side
- int size = len;
- while(size>0){
- //swap
- int tmp = data[size-1];
- data[size-1] = data[0];
- data[0] = tmp;
- size--; //only consider 0...size-1
- moveDown(data,0,size);
- }
- }
- /**
- * 用Floyd算法转换任何一颗完全二叉树为最大值堆(maxheap)
- *
- * Build a heap from any non-heap array
- * 转换数组为堆结构,无需开辟额外的空间(直接在数组中交换in-place)
- * Floyd Algorithm: O(n)
- *
- */
- //input: a randomly generated integer array
- //output: convert it into a maxheap
- private static void heapify(int[] data){
- if(data.length<2) return;
- //the last nonleaf node's index
- int nonLeafIndex = data.length/2 - 1;
- for(int i=nonLeafIndex;i>=0;i--){
- moveDown(data,i,data.length);
- }
- }
- //index: the nonleaf node's index
- //size: the size of array (from 0 to size-1) that is to be heapified
- private static void moveDown(int[] data,int index,int size){
- int target = data[index]; //save the target value to be moved down
- while(true){
- int leftChildIndex = 2*index + 1;
- int rightChildIndex = leftChildIndex+1;
- if(rightChildIndex>size-1){
- //if right child is empty, left child must be empty or leaf
- if(leftChildIndex<size){
- //left child is leaf
- if(target<data[leftChildIndex]){
- data[index] = data[leftChildIndex];
- index = leftChildIndex;
- }
- }
- break;
- }
- if(data[leftChildIndex]>data[rightChildIndex]){
- //left child is larger
- if(target<data[leftChildIndex]){
- //exchange the max with index node
- data[index] = data[leftChildIndex];
- index = leftChildIndex;
- }else{
- break;
- }
- }else{
- //right child is larger
- if(target<data[rightChildIndex]){
- data[index] = data[rightChildIndex];
- index = rightChildIndex;
- }else{
- break;
- }
- }
- }
- data[index] = target;
- }
- public static void main(String[] args) {
- int[] data = {2,6,12,17,9,1,0,3,4,8,7,10,5};
- HeapSort.solve(data);
- for(int i=0;i<data.length;i++){
- System.out.format(" %d",data[i]);
- }
- }
- }
#include "stdio.h"
#include "string.h"
void bubble_sort(int a[],int len)
{
int temp = 0;
for(int i = len-1;i>0;i--)
for ( int j = 0; j<i; j++)
{
if(a[j] >a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
void select_sort(int a[],int len)
{
int temp = 0;
int loc = 0;
for(int i = len-1;i>0;i--)
{
temp = a[i];
for ( int j = 0; j<i; j++)
{
if ( temp < a[j] )
{
loc = j;
temp = a[j];
}
}
a[loc] = a[i];
a[i] = temp;
}
}
void merge(int a[],int s,int m,int e)
{
int l = e-s+1;
int temp[l];
int i=s,j=m+1,k=0;
while(i<=m&&j<=e)
{
if ( a[i] < a[j] )
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
if(i > m)
{
for (;j<=e;j++)
temp[k++] = a[j];
}
if(j > e)
{
for(;i<=m;i++)
temp[k++] = a[i];
}
memcpy(a+s,temp,l*sizeof(int));
}
void mergesort(int a[],int s, int e)
{
if(s!=e)
{
mergesort(a,s,(s+e)/2);
mergesort(a,(s+e)/2+1,e);
merge(a,s,(s+e)/2,e);
}
}
void QuickSort(int a[],int numsize)/*a是整形数组,numsize是元素个数*/
{
int i=0,j=numsize-1;
int val=a[0];/*指定参考值val大小*/
if(numsize>1)/*确保数组长度至少为2,否则无需排序*/
{
while(i<j)/*循环结束条件*/
{
/*从后向前搜索比val小的元素,找到后填到a[i]中并跳出循环*/
for(;j>i;j--)
if(a[j]>val)
{
a[i]=a[j];
break;
}
/*从前往后搜索比val大的元素,找到后填到a[j]中并跳出循环*/
for(;i<j;i++)
if(a[i]<val)
{
a[j]=a[i];
break;
}
}
a[i]=val;/*将保存在val中的数放到a[i]中*/
QuickSort(a,i);/*递归,对前i个数排序*/
QuickSort(a+i+1,numsize-1-i);/*对i+1到numsize-1这numsize-1-i个数排序*/
}
}
int main()
{
int a[] = {10,9,8,7,6,5,4,3,2,1};
// for ( int i = 0; i<10; i++)
//printf("%d\n",a[i]);
// bubble_sort(a,10);
// for ( int i = 0; i<10; i++)
// printf("%d\n",a[i]);
mergesort(a,0,9);
for ( int i = 0; i<10; i++)
printf("%d\n",a[i]);
QuickSort(a,10);
for ( int i = 0; i<10; i++)
printf("%d\n",a[i]);
select_sort(a,10);
for ( int i = 0; i<10; i++)
printf("%d\n",a[i]);
return 0;
}