LintCode 463 Sort Integer

这个是O(n2)的排序的总结

/* bubble sort */
public static void sortIntegers(int[] A) {
// Write your code here
int len = A.length;

if (len == 0) return;

for (int j = 0; j < len; ++j) {
for (int i = 0; i < len-1-j; ++i) { // len-1-j
if (A[i] > A[i+1]) {
int temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
}
}

/* selection sort */
public static void sortIntegers(int[] A) {

for (int i = 0; i < A.length-1; i++){
int index = i;

for (int j = i+1; j < A.length; j++){
if (A[index] > A[j]) index=j; // find the smallest
}
// then swap
int smaller = A[index];
A[index] = A[i];
A[i] = smaller;
}
}

/* insertion sort */
public static void sortIntegers(int[] A) {
int n = A.length;
for (int i = 1; i < n; i++) {
int key = A[i]; // boundary

int j = i - 1; // another pointer
while ((j > -1) && (A[j] > key)) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = key;

}
}

时间: 2024-10-17 06:23:52

LintCode 463 Sort Integer的相关文章

zenefits oa - sort integer array in lexographical order

[ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ] Then the output should be: [ 1 | 100000 | 12 | 222 | 23 | 2434 | 56 | 654 ] 建立自己的comparator: Comparator<Integer> intLexCompare = new Comparator<Integer>() { int compareTo( Integer x, Integer y)

[LintCode] Roman to Integer

Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%

[LintCode] Roman to Integer 罗马数字转化成整数

Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Have you met this question in a real interview? Yes Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals htt

[lintcode easy]Reverse Integer

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Example Given x = 123, return 321 Given x = -123, return -321 \\\The length of Int data is from -2^31 to 2^31-1; [-2^31 , 2^31 -1] 即 [-2147483648,214

[LintCode] Insertion Sort List

Insertion Sort List Sort a linked list using insertion sort. Example Given 1->3->2->0->null, return 0->1->2->3->null. SOLUTION: 插入排序的思路就是从头开始扫过去,然后遇到比当前点小的点,就把这个点插到当前点之前.插入之后这次操作就完了,从头再扫一遍,直到扫到最后一个点位置. 代码: /** * Definition for List

Lintcode 49 Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Notice It's NOT necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "a

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

[LintCode] Wiggle Sort 扭动排序

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Notice Please complete the problem in-place. ExampleGiven nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

Merge Sort

package algorithm; public class MergeSort { public void merge(Integer[] array, Integer begin,Integer mid ,Integer end) { Integer[] newArray = new Integer[end + 1]; Integer sign = 0; while(begin <= end/2 && mid < end) { if(array[begin] < a