数据结构排序算法

Posted 黑胡子大叔的小屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构排序算法相关的知识,希望对你有一定的参考价值。

排序实现代码段:

直接插入排序

顺序表实现

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])
            //此时需要将其插入有序表
            //1、首先复制哨兵
            arr->data[0] = arr->data[i];
            
            for(j = i-1; arr->data[0] < arr->data[j]; j--)
                //2、后移,找到哨兵插入的正确位置
                arr->data[j+1] = arr->data[j];
            

            //3、插入哨兵
            arr->data[j+1] = arr->data[0];
        
    
    

链表实现

typedef struct LINKNODE

    int value;
    struct LINKNODE *next;
 *NODE;

//直接插入排序
void InsertSort(NODE head)

    NODE p,pre,h;

    //1、将原链表断裂成两个链表,一个为有序链表,另一个为待排序的链表,其中将原链表的第一个节点视为有序的链表
    h = head->next->next;
    head->next->next = NULL;

    //2、从待排序的链表中每次取一个节点插入到有序链表中
    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;
;

//希尔插入排序,dl为每次的步长
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])
            //此时需要将其插入有序表
            //1、首先复制哨兵
            arr->data[0] = arr->data[i];
            
            for(j = i-dl; arr->data[0] < arr->data[j]; j=j-dl)
                //2、后移,找到哨兵插入的正确位置
                arr->data[j+dl] = arr->data[j];
            

            //3、插入哨兵
            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;
    //1、遍历链表每次取最值与当前节点交换
    p = head->next;
    while(p != NULL)
    
        min = GetMin(p);
		//交换位置
        temp = p->value;
        p->value = min->value;
        min->value = temp;
    
        p = p->next;
    


测试代码汇总

顺序

其中需要用哨兵的算法使用init2()初始化顺序表,包括:直接插入、希尔、快速

/**
 * @file        testsqlist.c
 * @brief       顺序表实现排序
 *              
 * @author      UncleBb
 * @version     0.0.0.1
 * @date        2021/12/10
 */

#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])
            //此时需要将其插入有序表
            //1、首先复制哨兵
            arr->data[0] = arr->data[i];
            
            for(j = i-1; arr->data[0] < arr->data[j]; j--)
                //2、后移,找到哨兵插入的正确位置
                arr->data[j+1] = arr->data[j];
            

            //3、插入哨兵
            arr->data[j+1] = arr->data[0];
        
    
    


//希尔插入排序,dl为每次的步长
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])
            //此时需要将其插入有序表
            //1、首先复制哨兵
            arr->data[0] = arr->data[i];
            
            for(j = i-dl; arr->data[0] < arr->data[j]; j=j-dl)
                //2、后移,找到哨兵插入的正确位置
                arr->data[j+dl] = arr->data[j];
            

            //3、插入哨兵
            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<

以上是关于数据结构排序算法的主要内容,如果未能解决你的问题,请参考以下文章

算法排序之堆排序

快速排序-递归实现

TimSort算法分析

从搜索文档中查找最小片段的算法?

《数据结构与算法》十大经典排序算法动图演示(2019最新Python代码)

买什么数据结构与算法,这里有:动态图解十大经典排序算法(含JAVA代码实现)