排序实现代码段:
直接插入排序
顺序表实现
struct ARRAY_LIST{
int index;
int data[MAX_LEN];
};
void InsertSort(struct ARRAY_LIST *arr){
int i,j;
for(i = 2; i < arr->index; i++){
if(arr->data[i] < arr->data[i-1]){
arr->data[0] = arr->data[i];
for(j = i-1; arr->data[0] < arr->data[j]; j--){
arr->data[j+1] = arr->data[j];
}
arr->data[j+1] = arr->data[0];
}
}
}
链表实现
typedef struct LINKNODE
{
int value;
struct LINKNODE *next;
} *NODE;
void InsertSort(NODE head){
NODE p,pre,h;
h = head->next->next;
head->next->next = NULL;
while(h != NULL){
p = h->next;
pre = head;
while(pre->next != NULL && pre->next->value < h->value){
pre = pre->next;
}
h->next = pre->next;
pre->next = h;
h = p;
}
}
希尔排序
struct ARRAY_LIST{
int index;
int data[MAX_LEN];
};
struct SHELL_DL{
int dl[MAX_LEN];
int len;
};
void ShellInsert(struct ARRAY_LIST *arr,int dl){
int i,j;
for(i = dl+1 ; i < arr->index; i++){
if(arr->data[i] < arr->data[i-dl]){
arr->data[0] = arr->data[i];
for(j = i-dl; arr->data[0] < arr->data[j]; j=j-dl){
arr->data[j+dl] = arr->data[j];
}
arr->data[j+dl] = arr->data[0];
}
}
}
void ShellSort(struct ARRAY_LIST *arr,struct SHELL_DL *dl){
int i;
for(i = 0; i < dl->len; i++){
ShellInsert(arr,dl->dl[i]);
}
}
冒泡排序
struct ARRAY_LIST{
int index;
int data[MAX_LEN];
};
void BubbleSort(struct ARRAY_LIST *arr){
int i,j;
int temp;
printf("元素个数:%d\n",arr->index);
for(i = 0; i < arr->index; i++){
for(j = 0; j < arr->index-1-i; j++){
if(arr->data[j] < arr->data[j+1]){
temp = arr->data[j];
arr->data[j] = arr->data[j+1];
arr->data[j+1] = temp;
}
}
}
}
快速排序
struct ARRAY_LIST{
int index;
int data[MAX_LEN];
};
int GetCenter(struct ARRAY_LIST *arr,int low,int high){
int i;
printf("low: %d\nhigh:%d\n",low,high);
arr->data[0] = arr->data[low];
while(low < high){
while(low < high && arr->data[0] >= arr->data[high])
high--;
arr->data[low] = arr->data[high];
while(low < high && arr->data[0] <= arr->data[low])
low++;
arr->data[high] = arr->data[low];
}
arr->data[low] = arr->data[0];
printf("GetCenter Finish\n");
return low;
}
void QuickSort(struct ARRAY_LIST *arr,int low,int high){
int center;
center = GetCenter(arr, low, high);
printf("center: %d\nlow:%d\nhigh:%d\n",center,low,high);
if(low < high){
QuickSort(arr, center+1, high);
QuickSort(arr, low, center-1);
}
}
简单选择排序
顺序实现
void SelectSort(struct ARRAY_LIST *arr){
int i,j,k;
int temp;
for(i = 0; i < arr->index; i++){
k = i;
for( j = i; j < arr->index; j++){
if(arr->data[k] < arr->data[j])
k = j;
}
if(k != i){
temp = arr->data[i];
arr->data[i] = arr->data[k];
arr->data[k] = temp;
}
}
}
链表实现
typedef struct LINKNODE
{
int value;
struct LINKNODE *next;
} *NODE;
NODE GetMin(NODE h){
NODE min;
min = h;
while(h){
if(min->value > h->value){
min = h;
}
h = h->next;
}
return min;
}
void SelectSort(NODE head){
NODE p;
NODE min;
int temp;
p = head->next;
while(p != NULL){
min = GetMin(p);
temp = p->value;
p->value = min->value;
min->value = temp;
p = p->next;
}
}
测试代码汇总
顺序
其中需要用哨兵的算法使用init2()初始化顺序表,包括:直接插入、希尔、快速
#include <stdio.h>
#define MAX_LEN 20
struct ARRAY_LIST{
int index;
int data[MAX_LEN];
};
struct SHELL_DL{
int dl[MAX_LEN];
int len;
};
void init(struct ARRAY_LIST *arr){
if(NULL == arr){
return;
}
arr->index = 0;
}
void init2(struct ARRAY_LIST *arr){
if(NULL == arr){
return;
}
arr->index = 1;
arr->data[0] = 0;
}
void InitBl(struct SHELL_DL *dl){
dl->len = 3;
dl->dl[0] = 3;
dl->dl[1] = 2;
dl->dl[2] = 1;
}
void add(struct ARRAY_LIST *arr,int value){
if(arr->index == MAX_LEN){
printf("顺序表已满,无法插入!\n");
return;
}
arr->data[arr->index] = value;
arr->index ++;
}
void out(struct ARRAY_LIST *arr){
int i;
i = 0;
while (i < arr->index)
{
printf("%d ",arr->data[i]);
i++;
}
printf("\n");
}
void BubbleSort(struct ARRAY_LIST *arr){
int i,j;
int temp;
printf("元素个数:%d\n",arr->index);
for(i = 0; i < arr->index; i++){
for(j = 0; j < arr->index-1-i; j++){
if(arr->data[j] < arr->data[j+1]){
temp = arr->data[j];
arr->data[j] = arr->data[j+1];
arr->data[j+1] = temp;
}
}
}
}
void InsertSort(struct ARRAY_LIST *arr){
int i,j;
for(i = 2; i < arr->index; i++){
if(arr->data[i] < arr->data[i-1]){
arr->data[0] = arr->data[i];
for(j = i-1; arr->data[0] < arr->data[j]; j--){
arr->data[j+1] = arr->data[j];
}
arr->data[j+1] = arr->data[0];
}
}
}
void ShellInsert(struct ARRAY_LIST *arr,int dl){
int i,j;
for(i = dl+1 ; i < arr->index; i++){
if(arr->data[i] < arr->data[i-dl]){
arr->data[0] = arr->data[i];
for(j = i-dl; arr->data[0] < arr->data[j]; j=j-dl){
arr->data[j+dl] = arr->data[j];
}
arr->data[j+dl] = arr->data[0];
}
}
}
void ShellSort(struct ARRAY_LIST *arr,struct SHELL_DL *dl){
int i;
for(i = 0; i < dl->len; i++){
ShellInsert(arr,dl->dl[i]);
}
}
int GetCenter(struct ARRAY_LIST *arr,int low,int high){
int i;
printf("low: %d\nhigh:%d\n",low,high);
arr->data[0] = arr->data[low];
while(low < high){
while(low < high && arr->data[0] >= arr->data[high])
high--;
arr->data[low] = arr->data[high];
while(low < high && arr->data[0] <= arr->data[low])
low++;
arr->data[high] = arr->data[low];
}
arr->data[low] = arr->data[0];
printf("GetCenter Finish\n");
return low;
}
void QuickSort(struct ARRAY_LIST *arr,int low,int high){
int center;
center = GetCenter(arr, low, high);
printf("center: %d\nlow:%d\nhigh:%d\n",center,low,high);
if(low < high){
QuickSort(arr, center+1, high);
QuickSort(arr, low, center-1);
}
}
void SelectSort(struct ARRAY_LIST *arr){
int i,j,k;
int temp;
for(i = 0; i < arr->index; i++){
k = i;
for( j = i; j < arr->index; j++){
if(arr->data[k] < arr->data[j])
k = j;
}
if(k != i){
temp = arr->data[i];
arr->data[i] = arr->data[k];
arr->data[k] = temp;
}
}
}
int main(){
struct ARRAY_LIST arr;
struct SHELL_DL dl;
init(&arr);
add(&arr,5);
add(&arr,3);
add(&arr,4);
add(&arr,1);
add(&arr,10);
add(&arr,21);
add(&arr,13);
add(&arr,14);
printf("排序前:");
out(&arr);
BubbleSort(&arr);
printf("排序后:");
out(&arr);
}
链表
#include <stdio.h>
#include <stdlib.h>
typedef struct LINKNODE
{
int value;
struct LINKNODE *next;
} *NODE;
int headadd(struct LINKNODE *head,int value){
struct LINKNODE *node;
struct LINKNODE *p;
p = head->next;
node = malloc(sizeof(struct LINKNODE));
node->value = value;
head->next = node;
node->next = p;
}
int out(struct LINKNODE *linklist){
if(linklist == NULL){
printf("链表为空");
return 0;
}
struct LINKNODE *node;
node = linklist->next;
while (node != NULL)
{
printf("%d ",node->value);
node = node->next;
}
printf("\n");
}
struct LINKNODE *init(struct LINKNODE *head){
struct LINKNODE *p;
p = malloc(sizeof(struct LINKNODE));
if(p == NULL){
printf("Fail to malloc memory!\n");
return NULL;
}
head = p;
p->next = NULL;
return head;
}
int delete(struct LINKNODE **head){
NODE p;
while((*head) != NULL){
p = *head;
*head = (*head)->next;
free(p);
}
}
struct LINKNODE *add(struct LINKNODE *head,int value){
struct LINKNODE *node;
struct LINKNODE *p;
node = malloc(sizeof(struct LINKNODE));
node->value = value;
node->next = NULL;
p = head;
while(p->next != NULL){
p = p->next;
}
p->next = node;
return head;
}
int add2(struct LINKNODE **head,int value){
struct LINKNODE *node;
struct LINKNODE *p;
node = malloc(sizeof(struct LINKNODE));
node->value = value;
p = (*head)->next;
(*head)->next = node;
node->next = p;
}
void InsertSort(NODE head){
NODE p,pre,h;
h = head->next->next;
head->next->next = NULL;
while(h != NULL){
p = h->next;
pre = head;
while(pre->next != NULL && pre->next->value < h->value){
pre = pre->next;
}
h->next = pre->next;
pre->next = h;
h = p;
}
}
NODE GetMin(NODE h){
NODE min;
min = h;
while(h){
if(min->value > h->value){
min = h;
}
h = h->next;
}
return min;
}
void SelectSort(NODE head){
NODE p;
NODE min;
int temp;
p = head->next;
while(p != NULL){
min = GetMin(p);
printf("p节点:%d 最值节点:%d\n",p->value,min->value);
temp = p->value;
p->value = min->value;
min->value = temp;
out(head);
p = p->next;
}
}
int main(){
struct LINKNODE *head;
head = init(head);
printf("开始添加\n");
add2(&head,5);
add2(&head,3);
add2(&head,4);
add2(&head,1);
add2(&head,10);
add2(&head,21);
add2(&head,13);
add2(&head,14);
printf("添加完成\n");
out(head);
SelectSort(head);
printf("排序完成\n");
out(head);
delete(&head);
printf("释放完成\n");
out(head);
}
|