本次继续更新数据结构与算法这门课的上机实验,并且从本次开始将持续更新三篇关于排序和查找的内容——最根本的操作,主要涉及直接插入排序、快速排序(实验五)和 直接选择排序、两路并归排序(实验六)。
特此感谢, 实验过程中邓老师的指导和帮助!
对于想要获取此实验报告和源代码的同学,欢迎光顾小生寒舍 GitHub:https://github.com/ChromeWei?tab=repositories
实验内容
一、编写函数实现直接插入排序。
二、编写函数实现快速排序。
备注:鉴于方便大家对直接插入排序和快速排序的理解,我查找了很多相关CSDN平台上也没有写得较为细致的博文,所以我汇总了几篇,在这里引用他们的链接。(小生先谢过几位作者,如涉及侵权,亲联系我即删 🙏)
直接插入排序
- 排序算法之直接插入排序 https://blog.csdn.net/qq_37623612/article/details/80312121.
- Insertion Sort https://www.geeksforgeeks.org/insertion-sort/.
- 直接插入排序 一份课件https://wenku.baidu.com/view/83f94a205901020207409c39.html.
快速排序
- 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道,分类整理的题库,算法思路和源代码实现配套,各个类型总结出来的解题模板,远比你一个人要强!
MaiweiAI-com | WeChat ID: Yida_Zhang2
机器学习+计算机视觉