int getLeftPosition(int A[],int l,int r,int key)
{
int m;
while(r - l > 1)
{
m = l + (r - l)/2;
if(A[m] >= key)
r = m;
else
l = m;
}
return r;
}
int CountOccurances(int A[],int size,int key)
{
int left = getLeftPosition(A,-1,size-1,key);
int rigth = getRightPosition(A,0,size,key);
if(A[left] == key && key == A[right])
return right-right+1;
else
return 0;
}
2.选择排序
代码实现:
void selectionSort(int arr[],int n)
{
int i,j,min_index;
for(i = 0;i < n;i++)
{
min_index = i;
for(j = i;j < n;j++)
if(arr[j] < arr[min_index])
min_index = j;
swap(&arr[i],&arr[min_index]);
}
}
时间复杂度:O(n*n)
空间复杂度:O(1)
3.冒泡排序
代码:
void bubbleSort(int arr[],int n)
{
int i,j;
for(i = 0;i < n;i++)
for(j = 0;j < n-i-1;j++)
{
if(arr[j] > arr[j+1])
swap(&arr[j],&arr[j+1]);
}
}
4.插入排序
代码:
void insertionSort(int arr[],int n)
{
int i,key,j;
for(i = 1;i < n;i++)
{
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}
5.归并排序
代码:
void merge(int array[],int l,int m,int r)//将两个有序的数组合并
{
int n1 = m - l + 1;
int n2 = r - m;
int temp1[n1];
int temp2[n2];
int i,j,k;
for(i = 0;i < n1;i++)
temp1[i] = array[i+l];
for(j = 0;j < n2;j++)
temp2[j] = array[m+1+j];
i = 0;
j = 0;
k = l;
while(i < n1 && j < n2)
{
if(temp1[i] < temp2[j])
{
array[k] = temp1[i];
i++;
k++;
}
else
{
array[k] = temp2[j];
j++;
k++;
}
}
while(i < n1)
{
array[k] = temp1[i];
i++;
k++;
}
while(j < n2)
{
array[k] = temp2[j];
j++;
k++;
}
}
void mergeSort(int array[],int l,int r)
{
if(r - l > 0)
{
int m = l+(r-l)/2;
mergeSort(array,l,m);
mergeSort(array,m+1,r);
merge(array,l,m,r);
}
}
时间复杂度:O(nlg(n))
空间复杂度:O(n)
6.堆排序
思路:
Heap Sort Algorithm for sorting in increasing order:
1. Build a max heap from the input data.
2. At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap
by 1. Finally, heapify the root of tree.
3. Repeat above steps until size of heap is greater than 1
代码:
void maxHeapify(struct MaxHeap * maxHeap,int index)
{
int left = index * 2 + 1;//左子节点的下标
int right = index * 2 + 2;//右子节点的下标
int maxIndex = index;
if(left < maxHeap->size && maxHeap->array[left] > maxHeap->array[index])
maxIndex = left;
if(right < maxHeap->size && maxHeap->array[right] > maxHeap->array[maxIndex])
maxIndex = right;
if(maxIndex != index)
{
swap(&maxHeap->array[maxIndex],&maxHeap->array[index]);
maxHeapify(maxHeap,maxIndex);
}
}
struct MaxHeap * createAndBuildHeap(int * arr,int n)
{
int i;
struct MaxHeap * maxHeap = (struct MaxHeap*)malloc(sizeof(struct MaxHeap));
maxHeap->size = n;
maxHeap->array = arr;
for(i = (maxHeap->size/2)-1;i >= 0;i--)//从下标最大的父节点开始heapify操作
maxHeapify(maxHeap,i);
return maxHeap;
}
void headpSort(int arr[],int n)
{
struct MaxHeap * maxHeap = createAndBuildHeap(arr,n);
while(maxHeap->size > 1)
{
swap(&maxHeap->array[0],&maxHeap->array[maxHeap->size-1]);
maxHeap->size--;
maxHeapify(maxHeap,0);
}
}
7.快速排序
思路:每次根据比数组中最后一个元素大还是小将数组分为两个部分,然后递归地对这两个部分运用快排。
代码:
int partition(int array[],int l,int h)
{
int i = l - 1;
int target = array[h];
int j;
for(j = l;j <= h-1;j++)
{
if(array[j] <= target)
{
i++;
swap(&array[i],&array[j]);
}
}
swap(&array[i+1],&array[h]);
return i+1;
}
void quickSort(int arr[],int l,int h)
{
if(h > l)
{
int pos = partition(arr,l,h);
quickSort(arr,l,pos-1);
quickSort(arr,pos+1,h);
}
}
时间复杂度:O(nlg(n))
8.希尔排序
代码:
void shellSort(int arr[],int n)
{
int gap,i,j,temp;
for(gap = n/2;gap > 0;gap = gap/2)
{
for(i = gap;i < n;i++)
{
temp = arr[i];
for(j = i;j >= gap && arr[j - gap] > temp;j = j - gap)
arr[j] = arr[j - gap];//插入排序
arr[j] = temp;
}
}
}