- 直接插入排序
定义:将第i个记录插入到i-1个有序记录中
平均时间复杂度o(n2)
#include<stdio.h>
#define MAX 8
int main(void)
{
int a[MAX]={8,7,6,5,4,3,2,1};
int i;
void insert(int*a,int n);
insert(a,MAX);
printf("----排序完成后----\n");
for(i=0;i<8;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
void insert(int *a,int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
key=a[i];
for(j=i;j>0 && a[j-1]>key;j--)
{
a[j] = a[j-1];
}
a[j] = key;
}
}
打印输出:
----排序完成后----
1 2 3 4 5 6 7 8
void bubbleSort(int nums[],int length);
void swap(int nums[],int i , int j);
void printArray(int nums[],int length);
int main(int argc, const char * argv[]) {
int nums[8] = {99,12,88,34,5,44,12,100};
int length = sizeof(nums) / sizeof(nums[0]);
printArray(nums, length);
printf("----排序完成后----\n");
bubbleSort(nums, length);
printArray(nums, length);
return 0;
}
void bubbleSort(int nums[],int length){
for (int i = 0; i < length - 1 ; i ++) {
for (int j = 0; j < length -1 - i; j ++) {
if (nums[j] > nums[j+1]) {
int temp = nums[j] ;
nums[j] = nums[j+1];
nums[j+1] = temp;
}
}
}
}
void swap(int nums[],int i , int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
void printArray(int nums[],int length){
for (int i = 0; i <length; i++) {
printf("%i,",nums[i]);
}
}
打印输出:
99,12,88,34,5,44,12,100
----排序完成后----
5,12,12,34,44,88,99,100,
void selectSort(int nums[],int length);
void swap(int nums[],int i , int j);
void printArray(int nums[],int length);
int main(int argc, const char * argv[]) {
int nums[8] = {99,12,88,34,5,44,12,100};
int length = sizeof(nums) / sizeof(nums[0]);
printArray(nums, length);
printf("----排序完成后----\n");
selectSort(nums, length);
printArray(nums, length);
return 0;
}
void selectSort(int nums[],int length){
int i,j,temp,min;
for (i = 0; i< length ; i++) {
min = i;
for (j = i+ 1; j <length; j ++) {
if (nums[j] <nums[min]) {
min = j;
}
}
if (min !=i) {
temp = nums[min];
nums[min] = nums[i];
nums[i] = temp;
}
}
}
void swap(int nums[],int i , int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
void printArray(int nums[],int length){
for (int i = 0; i <length; i++) {
printf("%i,",nums[i]);
}
}
打印输出:99,12,88,34,5,44,12,100
----排序完成后----
5,12,12,34,44,88,99,100
#include <stdio.h>
void sort(int *a, int left, int right);
void swap(int nums[],int i , int j);
void printArray(int nums[],int length);
int main(int argc, const char * argv[]) {
int nums[8] = {99,12,88,34,5,44,22,100};
int length = sizeof(nums) / sizeof(nums[0]);
printArray(nums, length);
printf("----排序完成后----\n");
sort(nums, 0, 7);
printArray(nums, length);
return 0;
}
void sort(int *a, int left, int right)
{
if(left >= right)
{
return ;
}
int i = left;
int j = right;
int key = a[left];
while(i < j)
{
while(i < j && key <= a[j])
{
j--;
}
a[i] = a[j];
while(i < j && key >= a[i])
{
i++;
}
a[j] = a[i];
}
a[i] = key;
sort(a, left, i - 1);
sort(a, i + 1, right);
}
void printArray(int nums[],int length){
for (int i = 0; i <length; i++) {
printf("%i,",nums[i]);
}
}
打印输出:
99,12,88,34,5,44,22,100
----排序完成后----
5,12,22,34,44,88,99,100
#include <stdio.h>
int findKey(int nums[],int length,int key);
int main(int argc, const char * argv[]) {
int nums[8] = {8,12,15,19,23,25,33,55};
int length = sizeof(nums) / sizeof(nums[0]);
int key = 19;
int index = findKey(nums, length, key);
printf("index = %i\n",index);
return 0;
}
int findKey(int nums[],int length,int key){
int min,max,mid;
min = 0;
max = length - 1;
mid = (min + max) / 2;
while (key != nums[mid]) {
if (key > nums[mid]) {
min = mid + 1;
}else if (key < nums[mid]){
max = mid - 1;
}
if (min > max) {
return -1;
}
mid = (min + max) / 2;
}
return mid;
}
打印输出:index = 3
#include <stdio.h>
void shellsort(int a[], int n);
void printArray(int nums[],int length);
int main(int argc, const char * argv[]) {
int nums[8] = {99,12,88,34,5,44,22,100};
int length = sizeof(nums) / sizeof(nums[0]);
printArray(nums, length);
printf("----排序完成后----\n");
shellsort(nums, 8);
printArray(nums, length);
return 0;
}
void shellsort(int a[], int n)
{
int j, gap;
for (gap = n / 2; gap > 0; gap /= 2)
for (j = gap; j < n; j++)
if (a[j] < a[j - gap])
{
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
}
void printArray(int nums[],int length){
for (int i = 0; i <length; i++) {
printf("%i,",nums[i]);
}
}
打印输出:99,12,88,34,5,44,22,100
----排序完成后----
5,12,22,34,44,88,99,100