Data structure of the experimental order of a: a row of fast row(learning quick sorting).
来源:互联网 发布:c语言memset 编辑:程序博客网 时间:2024/06/03 15:32
Problem Description
给定N个长整型范围内的整数,要求输出以给定数据中第一个数为枢轴进行一趟快速排序之后的结果。
Input
Output
输出一趟快速排序后的结果,数字间以一个空格间隔,行末不得有多余空格。
Example Input
849 38 65 97 76 13 27 49
Example Output
27 38 13 49 76 97 65 49
This topic is obviously in the practice time complexity is O (nlogn) sorting method, we take this opportunity to talk about a commonly used internal sorting method: fast sort.
Quick sorting is an improvement to the bubble sort.The basic idea is that the sorted records are divided into separate parts by a sort of sorting, and some of the recorded keywords are smaller (or equal) than the other recorded keywords, and the two parts can be recorded separatelySorted to achieve the entire sequence.
Let us assume that the number of sequences to be sorted is {a [l], a [i + 1], a [l + 2], ..., a [r]} first take a record (we select the first position in the sequence) As the standard keyword, and then rearrange the remaining records, all the keywords less than or equal to its records are placed in the left child sequence, all keywords greater than or equal to it are placed in the right child sequence.This allows the sequence to be divided into two sub-sequences (the value of any keyword in the left child sequence is less than or equal to the value of any keyword in the right subsequence).
The specific approach is: with two pointers i and j, their initial values were l and r, the first record in the sequence of the keyword m, the first from the location of j refers to the first search to find the first The keyword is smaller than the record of m, and then search back from the location of i, find the first keyword is greater than m records, exchange them with each other, repeat these two steps until i> j .
AC code is as follows:
#include<stdio.h>#include<stdlib.h>int ksort(int a[],int low,int high){ int key=a[low]; int t; while(low<high) { while(low<high&&a[high]>=key) --high; { t=a[low]; a[low]=a[high]; a[high]=t; } while(low<high&&a[low]<=key) ++low; { t=a[low]; a[low]=a[high]; a[high]=t; } } return low;}int main(){ int a[100000],i,n; while(~scanf("%d",&n)) { for(i=0;i<n;i++) scanf("%d",&a[i]); ksort(a,0,n-1); for(i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d\n",a[n-1]); } return 0;}
The time complexity of fast sorting is O (nlogn), fast, but it is an unstable sorting method.If the initial record sequence is ordered or ordered, the fast sort will degrade into a bubble sort, and its time complexity is O (n ^ 2).So we generally use the median position of the record of the keyword as a standard keyword, which can simplify the code, but also to prevent degradation.Fast sorting requires a stack of spaces to implement recursion.
- Data structure of the experimental order of a: a row of fast row(learning quick sorting).
- Fast ways in R to get the first row of a data frame grouped by an identifier
- rowid format and the format of a row piece
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- Thoughts of learning data structure
- Structure of a spider
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- A problem of sorting
- The structure of a class file
- Oracle: check the structure of a table
- The structure of a PKZip file
- Getting Approximate Row Count of a Huge File
- Is there a limit to the number of columns in an HBase row?
- How to get the Values of Selected Row from a Gridview using ASP.NET
- Experimental Security Analysis of a Modern Automobile
- hdu 3999The order of a Tree
- Hdu 3999 - The order of a Tree
- HDU3999:The order of a Tree
- dp问题中记录路径的方法
- 合并果子解题报告
- linux常用命令总结
- 实例说明optimize table在优化mysql时很重要
- VC++6.0中添加批量注释和取消批量注释快捷键
- Data structure of the experimental order of a: a row of fast row(learning quick sorting).
- 面试一:Context 的理解
- android实现计算器(慕课网视频教学)
- springmvc 路径映射规则、数据绑定
- python pip install 异常问题
- 【java】Collection常用操作
- appium+python+夜神模拟器+jenkins进行android端自动化测试(2)
- HDU-2476-String painter
- Scratch2.0编程-游戏制作-满屏飞的巫婆