Sorting Algorithms

2024-06-02 08:32
文章标签 algorithms sorting

本文主要是介绍Sorting Algorithms,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

[TOC]

PART 1 内排序

插入排序

** 直接插入排序
**二分插入排序
**希尔排序

交换排序

** 冒泡排序
** 快速排序



交换排序

冒泡排序

基本思想:

通过无序区中相邻记录关键字间的比较和位置的交换,使关键字最小的(最大的)记录如气泡一般逐渐往上“漂浮”直至“水面”。

给出“两种”形式的冒泡,自行斟酌

大者上浮

void BubbleSort(int[] arr){int n = arr.length;for(int i = 0; i < n; i++){for(int j = 0; j < n-i-1; j++){if(arr[j]>arr[j+1]){int tmp = arr[j];arr[j] = arr[j+1];arr[j+1] = tmp;}}}
}

小者下沉

void BubbleSort(int[] arr){int n = arr.length;for(int i = 0; i < n; i++){for(int j = n-1; j > i; j--){if(arr[j]<arr[j-1]){int tmp = arr[j];arr[j] = arr[j-1];arr[j-1] = tmp;}}}
}

快速排序 , 首先快排是由冒泡排序改进而来。

快排思想:
在待排序的n个记录中任取一个记录(通常第一个),把该记录放入适当的位置后,数据序列被划分成两份。所有关键字比该记录关键字小的记录放在前一部分,比它大的记录放置在后一部分,并把该记录放在两部分中间(称为该记录归位),这个过程称做一趟快速排序。之后对所有的两部分重复上述的过程,直至每部分内只有一个记录或空为止。

简而言之,每趟使表的第一个元素放入适当位置,将表一分为二,对子表按递归方式继续划分,直至划分的子表长度为1或0。

以第一个记录为分割点

void QuickSort(int[] arr,int low, int high){int i = low, j = high;if(low < high) {                   //至少两个元素int pivot = arr[low];         while(i!=j) {                  //while(j > i && arr[j] > pivot)   //从右向左扫描 找比pivot小的元素j--;arr[i] = arr[j];                 while(i < j && arr[i] < pivot)   //从左往右扫描i++;arr[j] = arr[i];}arr[i] = pivot;sort(arr, low, i-1);sort(arr, i+1, high);}}

Another:
以最后一个为分割点

/* This function takes last element as pivot,places the pivot element at its correctposition in sorted array, and places allsmaller (smaller than pivot) to left ofpivot and all greater elements to rightof pivot */int partition(int arr[], int low, int high) {int pivot = arr[high];int i = low -1;// index of smaller elementint tmp;if(low<high) {          for(int j = low; j < high; j++) {// If current element is smaller than or// equal to pivotif(arr[j] < pivot) {//swap arr[i] and arr[j]i++;tmp = arr[j];arr[j] = arr[i];arr[i] = tmp;}}           //swap arr[i+1] and arr[high](pivot)tmp = arr[i+1];arr[i+1] = arr[high];arr[high] = tmp;            }return i+1;}/* The main function that implements QuickSort()arr[] --> Array to be sorted,low  --> Starting index,high  --> Ending index */void sort(int[] arr, int low, int high) {/* pi is partitioning index, arr[pi] is now at right place */int pi = partition(arr, low, high);// Recursively sort elements before// partition and after partitionsort(arr, low, pi-1);sort(arr, pi+1, high);}

选择排序

直接选择
堆排序

直接选择

基本思想:
第i趟排序开始时,当前有序区和无序区R[0..i-1]和R[i..n-1] (0<= i =>n-1),该趟排序则是从当前无序区中选出关键字最小的记录R[k],将它与无序区的第一个记录R[i]交换,使R[0..i] 和 R[i+1 .. n-1]分别变为新的有序区和新的无序区,即第i趟之后,R[0..i]有序区中的关键字均小于等于R[i+1..n-1]中所有关键字,所以进行n-1趟排序后,R[0..n-2]的所有关键字小于等于R[n-1],也就是n-1趟之后,整个表R[0..n-1]递增有序。

这篇关于Sorting Algorithms的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1023456

相关文章

torch.optim 之 Algorithms (Implementation: for-loop, foreach, fused)

torch.optim的官方文档 官方文档中文版 一、Implementation torch.optim的官方文档在介绍一些optimizer Algorithms时提及它们的implementation共有如下三个类别:for-loop, foreach (multi-tensor), and fused。 Chat-GPT对这三个implementation的解释是: For-loo

POJ3270 cow sorting 【polya】

题目描述: 给你一个数字序列(每个数字唯一),每次你可以交换任意两个数字,代价为这两个数字的和,问最少用多少代价能把这个序列按升序排列好。 题目的具体做法是参考刘汝佳的《算法艺术与信息学奥赛》大概思路是:以后再用别种方法解, 1.找出初始状态和目标状态。明显,目标状态就是排序后的状态。 2.画出置换群,在里面找循环。例如,数字是8 4 5 3 2 7 明显,

POJ1486 Sorting Slides 二分图最大匹配 必要匹配

http://poj.org/problem?id=1486 题意:读题读得很纠结~~ 大意就是平面坐标上有一系列的矩形(各边都和坐标轴平行)和 一些点;每个矩形和在他之内的点对应; 然后找出那些绝对匹配(就是在任何最大匹配中,某个矩形和某个点始终对应); 所谓必要匹配在本题中的意思就是,在所有的最大匹配中,1个数字都会匹配到同一个字母上去。数字x只能与字母y匹配

poj3270 Cow Sorting 置换群

题意:有一个序列,你需要将通过交换使得序列最后单调增。每次交换的代价为交换的两个数的和,求将序列变成单调增地最小代价 和。 思路:如果有一个序列:8 4 5 3 2 7 ,那么最终的序列为:2 3 4 5 7 8 。如果视作是一个置换群的作用,那么可以写出循环之积: (8 2 7)(4 3 5)。对于每个循环,每个数都至少被交换一次,所以我们应该用循环中的最小值和每个数交换,设循环数的最小值

九度oj-1041-Simple Sorting

时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4515 解决:1688 题目描述: You are given an unsorted array of integer numbers. Your task is to sort this array and kill possible duplicated elements occurring in it.

GNN algorithms(7): Flash Attention

目录 Background 参考  Flash Attention: Fast and Memory-Efficient Exact Attention with IO-Awareness Background HBM: high Boardwidth memory, 高带宽内存  SRAM: Static RAM, 静态随机存储器 Flash Attention 分而治之的思想

【索引】Geometric Computations and Algorithms in 3D

AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 4. Geometry::Geometric Computations and Algorithms in 3D ExamplesExamples:BeginnerExamples:IntermediateExamples

【索引】Geometric Algorithms in 2D

AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 4. Geometry::Geometric Algorithms in 2D ExamplesExamples:BeginnerExamples:IntermediateExamples:Advanced

zoj 1060 poj 1094 Sorting It All Out

题意:将给定的关系按从小到大排成一个唯一的序列。 思路:没输入一条边,使用拓扑排序判断能否得到最终结果,拓扑排序的结果有三种情形。 ⑴如果该图存在环,那么给定的关系肯定互相矛盾。 ⑵如果不存在环,但是拓扑排序结束后,排序得到的序列中的元素个数小于给定的元素个数,那么给定的关系不足以判断出全部元素的大小关系。 ⑶如果拓扑出来的序列中的元素个数等于给定的元素个数,那么给出的关系可以判断出

【索引】Chapter 1. Algorithm Design :: Designing Efficient Algorithms :: Exercises: Beginner

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=461  TitleTotal Submissions / Solving %Total Users / Solving %10125 - Sumsets17713 21.28% 2628 74.05% 1