LintCode: Sort Integers

Sort Integers



Similar Problems:


Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n^2) algorithm.

Example

Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// Blog link: https://code.dennyzhang.com/sort-integers
// Basic Ideas: bubble sort
// Complexity: Time O(n*n), Space O(1)
/**
 * @param A: an integer array
 * @return: nothing
 */
func sortIntegers (A *[]int)  {
    if len(*A) == 0 { return }
    for i:=0; i<len(*A)-1; i++{
        for j:=i+1; j<len(*A); j++ {
            if (*A)[i]>(*A)[j] {
                (*A)[i], (*A)[j] = (*A)[j], (*A)[i]
            }
        }
    }
}
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.