数据结构与算法上机实验五 排序(一)

GitHub 微信公众号 知乎 B站 CSDN

实验五 排序(一)

本次继续更新数据结构与算法这门课的上机实验,并且从本次开始将持续更新三篇关于排序和查找的内容——最根本的操作,主要涉及直接插入排序快速排序(实验五)和 直接选择排序两路并归排序(实验六)。
特此感谢, 实验过程中邓老师的指导和帮助!


对于想要获取此实验报告和源代码的同学,欢迎光顾小生寒舍 GitHub:https://github.com/ChromeWei?tab=repositories

实验内容

一、编写函数实现直接插入排序。
二、编写函数实现快速排序。

备注:鉴于方便大家对直接插入排序和快速排序的理解,我查找了很多相关CSDN平台上也没有写得较为细致的博文,所以我汇总了几篇,在这里引用他们的链接。(小生先谢过几位作者,如涉及侵权,亲联系我即删 🙏)

直接插入排序

  1. 排序算法之直接插入排序 https://blog.csdn.net/qq_37623612/article/details/80312121.
  2. Insertion Sort https://www.geeksforgeeks.org/insertion-sort/.
  3. 直接插入排序 一份课件https://wenku.baidu.com/view/83f94a205901020207409c39.html.

快速排序

  1. QuickSort https://www.geeksforgeeks.org/quick-sort/.

本篇重点: 但是最为推荐的是GeeksforGeeks官网链接这个平台在这里插入图片描述,不止有较为详尽的算法介绍,图文都会比较直观,掌握数据结构和算法等知识足够!!
链接:https://www.geeksforgeeks.org/sorting-algorithms/.
在这里插入图片描述

附件: 源代码

(测试环境:Win10, VisualC++ 6.0)

直接插入排序

/*************A.Insert sort for integers array deal *************
 Editor:Zhang Tony
 Date:2017/7/15
 Decription:Insert sort to sort the integers array 
 ***************************************************************/ 

// C program for insertion sort
#include <stdio.h>
#include <math.h>
 
/* Function to sort an array using insertion sort*/
void insertionSort(int arr[], int n)
{
   int i, key, j;
   for (i = 1; i < n; i++)
   {
       key = arr[i];
       j = i-1;
 
       /* Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
       while (j >= 0 && arr[j] > key)
       {
           arr[j+1] = arr[j];
           j = j-1;
       }
       arr[j+1] = key;
   }
}
 
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
   int i;
   for (i=0; i < n; i++)
       printf("%d ", arr[i]);
   printf("\n");
}
 
/* Driver program to test insertion sort */
int main()
{
    int arr[] = {12, 11, 13, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    printf("Orignial array: \n");
    printArray(arr, n);
    
    printf( "Sorted array: \n");
	insertionSort(arr, n);
    printArray(arr, n);
 
    return 0;
}


快速排序

/*************A.QuickSort for integers array deal *************
 Editor:Zhang Tony 
 Date:2017/7/16 
 Decription:QuickSort to sort the integers array 
 ***************************************************************/

/* C implementation QuickSort */
#include<stdio.h>
 
// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}
 
/* This function takes last element as pivot, places
   the pivot element at its correct position in sorted
    array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right
   of pivot */
int partition (int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot
    int i = (low - 1);  // Index of smaller element
    int j; 
	 
    for (j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}
 
/* The main function that implements QuickSort
  arr[] --> Array to be sorted,
  low  --> Starting index,
  high  --> Ending index */
void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        int pi = partition(arr, low, high);
 
        // Separately sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
 
/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
 
// Driver program to test above functions
int main(void)
{
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quickSort(arr, 0, n-1);
    printf("Sorted array: \n");
    printArray(arr, n);
    
    return 0;
}


上一篇:数据结构与算法上机实验四 结构数组

下一篇:数据结构与算法上机实验六 排序(二)


欢迎各位订阅我,谢谢大家的点赞和专注!我会继续给大家分享我大学期间详细的实践项目。

在这里插入图片描述
在这里插入图片描述

微信扫一扫, 关注我


知识星球

专为求职面试中算法与数据结构的小伙伴,创了学习交流/刷题群(知识星球)!想要最快的提升算法与数据结构技能,和更多小伙伴一起来吧!

进群获取互联网大厂高频coding题库,告别刷题400道,分类整理的题库,算法思路和源代码实现配套,各个类型总结出来的解题模板,远比你一个人要强!

Click to see the more details

MaiweiAI-com | WeChat ID: Yida_Zhang2

机器学习+计算机视觉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Charmve

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值