464. 整数排序 II
2017-12-11 22:14
246 查看
给一组整数,按照升序排序。使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法。
您在真实的面试中是否遇到过这个题?
Yes
样例
给出
排序后的结果为
public class Solution {
/*
* @param A: an integer array
* @return:
*/
public void sortIntegers2(int[] A) {
// write your code here
Wsort(A, 0, A.length-1);
}
public int partiton(int[] A, int low, int high)
{
int first = low;
int last = high;
int key = A[low];
while(first < last)
{
while(first < last && A[last] >= key)
{
last--;
}
if(first < last) A[first] = A[last];
while(first < last && A[first] <= key)
{
first++;
}
if(first < last) A[last] = A[first];
}
A[first] = key;
return first;
}
public void Wsort(int[] A, int low, int high)
{
if (low < high)
{
int index = partiton(A, low, high);
Wsort(A, low, index-1);
Wsort(A, index+1, high);
}
}
}
您在真实的面试中是否遇到过这个题?
Yes
样例
给出
[3, 2, 1, 4, 5],
排序后的结果为
[1, 2, 3, 4, 5]。
public class Solution {
/*
* @param A: an integer array
* @return:
*/
public void sortIntegers2(int[] A) {
// write your code here
Wsort(A, 0, A.length-1);
}
public int partiton(int[] A, int low, int high)
{
int first = low;
int last = high;
int key = A[low];
while(first < last)
{
while(first < last && A[last] >= key)
{
last--;
}
if(first < last) A[first] = A[last];
while(first < last && A[first] <= key)
{
first++;
}
if(first < last) A[last] = A[first];
}
A[first] = key;
return first;
}
public void Wsort(int[] A, int low, int high)
{
if (low < high)
{
int index = partiton(A, low, high);
Wsort(A, low, index-1);
Wsort(A, index+1, high);
}
}
}
相关文章推荐
- 整数排序 II
- lintcode-464-整数排序 II
- lintcode 整数排序 II | 快速排序(Java)
- Lintcode - 整数排序 II
- lintcode 463整数排序 lintcode 464整数排序II
- LintCode-整数排序 II
- 整数排序 II
- 整数排序 II
- LintCode 整数排序 II
- 464.整数排序 II
- lintcode_整数排序 II
- lintcode 整数排序 II | 归并排序(Java)
- LintCode 464 整数排序 II
- 整数排序 II
- 整数排序 II
- [LintCode] Sort Integers II 整数排序之二
- 整数排序 II
- LinkCode 整数排序II
- Lintcode——整数排序 II
- 整数排序 II