排序
文章平均质量分 61
张荣华_csdn
这个作者很懒,什么都没留下…
展开
-
插入排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> insert(vector<int> nums){ int n = nums.size(); for (int i = 1; i <...原创 2018-06-03 00:03:50 · 151 阅读 · 0 评论 -
Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6,4] and k = 2Output: 5Example ...原创 2018-06-06 08:16:10 · 147 阅读 · 0 评论 -
局部最小值位置
定义局部最小的概念。arr长度为1时,arr[0]是局部最小。arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小;如果arr[N-1]<arr[N-2],那么arr[N-1]是局部最小;如果0<i<N-1,既有arr[i]<arr[i-1]又有arr[i]<arr[i+1],那么arr[i]是局部最小。 给定无序数组a...原创 2018-06-07 08:55:48 · 829 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.Example 1:Input: [10,2]Output: "210"Example 2:Input: [3,30,34,5,9]Output: "9534330"Note: The result may be v...原创 2018-07-12 00:17:40 · 126 阅读 · 0 评论 -
数组中的逆序对
题目:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。例如,在数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6)、(7,5)、(7,4)、(6,4)、(5,4)。统计逆序对的过程:先把数组分隔成子数组,统计出子数组内部的逆序对的数目,然后在统计出两个相邻子数组之间的逆序对的数目。在统计逆序对的过程中,还需要...原创 2018-07-20 00:15:19 · 147 阅读 · 0 评论 -
快速排序的性能分析
快速排序算法的平均时间复杂度为O(nlogn),通常认为在所有同数量级的排序算法中,快速排序的平均性能是最好的,这也是它被称为“快速排序”的原因。 具体到实际的快速排序操作,它的时间复杂度与基准元素的选取有关。如果每趟排序都将大部分元素划分到基准元素的一侧,那么快速排序将退化为冒泡排序,时间复杂度为O(n^2);一种更为特殊的情形就是序列初始化状态按关键字排序,而每趟排序选取的基准元素为当前子序...原创 2018-07-24 09:09:52 · 2199 阅读 · 0 评论 -
计数排序
对于一个int数组,请编写一个计数排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class CountingSort {public: int max,min; void max_min(int*A, int n, int &max, int &a...原创 2018-08-07 00:04:33 · 136 阅读 · 0 评论 -
基数排序
对于一个int数组,请编写一个基数排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。保证元素均小于等于2000。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class RadixSort {public: int* radixSort(int* A, int n) { // write code...原创 2018-08-07 00:04:39 · 151 阅读 · 0 评论 -
字符串排序
编写一个程序,将输入字符串中的字符按如下规则排序。规则 1 :英文字母从 A 到 Z 排列,不区分大小写。 如,输入: Type 输出: epTy规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。 如,输入: BabA 输出: aABb规则 3 :非英文字母的其它字符保持原来的位置。 如,输入: By?e 输出: Be?...原创 2018-08-07 00:05:20 · 643 阅读 · 0 评论 -
查找兄弟单词
输入描述:先输入字典中单词的个数n,再输入n个单词作为字典单词。再输入一个单词,查找其在字典中兄弟单词的个数m再输入数字k输出描述:根据输入,输出查找到的兄弟单词的个数m然后输出查找到的兄弟单词的第k个单词。#include<iostream>#include<string>#include<vector>#include<...原创 2018-08-08 10:25:37 · 203 阅读 · 0 评论 -
Sort List
Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4Example 2:Input: -1->5->3->4->0Output: -1->0->...原创 2018-06-05 08:06:38 · 165 阅读 · 0 评论 -
常见排序算法整理1(C++实现)
1. 冒泡排序时间复杂度O(n2)空间复杂度O(1) 算法的基本思想:相邻元素交换,一共n-1轮,每轮交换n-1-i个元素 C++实现:int* bubbleSort(int* A, int n) { for(int i=0;i<n-1;i++) { for(int j=0;j<n-1-i;j++) { ...原创 2018-04-18 14:55:35 · 186 阅读 · 0 评论 -
堆排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;void max_heapify(vector<int> &nums, int beg, int end){ int curr = beg; int child =...原创 2018-06-04 00:00:43 · 112 阅读 · 0 评论 -
归并排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;void mergesort(vector<int>& nums, int left, int mid, int right){ int n = right - left + ...原创 2018-06-04 00:59:35 · 152 阅读 · 0 评论 -
快速排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;int partition(vector<int> &nums, int left, int right){ int pivot = nums[left]; whi...原创 2018-06-03 00:04:07 · 119 阅读 · 0 评论 -
冒泡排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> bubble(vector<int> nums){ int n = nums.size(); for (int i = 0; i <...原创 2018-06-04 00:59:42 · 321 阅读 · 0 评论 -
希尔排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> shell(vector<int> nums){ int n = nums.size(); int h = n / 2; w...原创 2018-06-04 00:59:50 · 112 阅读 · 0 评论 -
选择排序
#include<iostream>#include<vector>#include<algorithm>using namespace std;vector<int> choose(vector<int> nums){ int n = nums.size(); for (int i = 0; i &l...原创 2018-06-04 01:09:04 · 154 阅读 · 0 评论 -
小范围排序
已知一个几乎有序的数组,几乎有序是指,如果把数组排好顺序的话,每个元素移动的距离可以不超过k,并且k相对于数组来说比较小。请选择一个合适的排序算法针对这个数据进行排序。给定一个int数组A,同时给定A的大小n和题意中的k,请返回排序后的数组。class ScaleSort {public: vector<int> sortElement(vector<int> A, ...原创 2018-05-16 14:35:48 · 144 阅读 · 0 评论 -
三色排序(荷兰国旗问题)
class ThreeColor {public: vector<int> sortThreeColor(vector<int> A, int n) { // write code here int left=0; int right=n-1; for(int i=0;i<n&&i<=...原创 2018-05-16 11:59:28 · 392 阅读 · 0 评论 -
常见排序算法整理2(C++实现)
1. 归并排序时间复杂度:O(nlogn)空间复杂度:O(n) 算法的基本思想:将每两个相邻元素进行归并,得到新的归并数组,每两个一组再次进行归并排序,直到所有元素均已排序。 C++实现:void mergehelp(int* A,int left,int mid,int right) { int* B=new int[right-left+1]; int i...原创 2018-04-18 15:32:38 · 235 阅读 · 0 评论 -
Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5,6,4] and k = 2 Output: 5Examp...原创 2018-08-05 00:22:35 · 141 阅读 · 0 评论