Interview Sort Function

QuickSort

Java Code:

 1 import java.util.*;
 2 public class quickSort{
 3     public static void main(String [] args){
 4         int [] arr = new int[]{4,2,7,5,0,9,1};
 5         int low = 0;
 6         int high = arr.length-1;
 7         quickSort(arr, low, high);
 8         System.out.println(Arrays.toString(arr));
 9     }
10
11     private static void quickSort(int [] arr, int low, int high){
12         if(arr == null || arr.length == 0){
13             return;
14         }
15
16         if(low >= high){
17             return;
18         }
19
20         int pivot = arr[low + (high - low)/2];
21
22         //make left < pivot, right > pivot
23         int i = low;
24         int j = high;
25         while(i <= j){
26             while(arr[i] < pivot){
27                 i++;
28             }
29             while(arr[j] > pivot){
30                 j--;
31             }
32
33             if(i <= j){
34                 int temp = arr[i];
35                 arr[i] = arr[j];
36                 arr[j] = temp;
37                 i++;
38                 j--;
39             }
40         }
41
42         //recursively sort two sub arrays
43         quickSort(arr, low, j);
44         quickSort(arr, i, high);
45     }
46 }
时间: 2024-10-12 13:19:24

Interview Sort Function的相关文章

c++ class as sort function

// constructing sets #include <iostream> #include <set> #include <string.h> bool fncomp (int lhs, int rhs) {return lhs<rhs;} struct classcomp { bool operator() (const int& lhs, const int& rhs) const {return lhs<rhs;} }; int

Sort Colors II Lintcode

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. Notice You are not suppose to use the library's sort function for this pro

75. Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

javascript sort

var info=JSON.parse(body); info.sort(function(a, b) { return parseInt(a[0]) > parseInt(b[0]) ? 1 : parseInt(a[0]) == parseInt(b[0]) ? 0 : -1; }); JS 排序代码

关于sort排序

JavaScript的数组排序函数 sort方法,默认是按照ASCII 字符顺序进行升序排列.arrayobj.sort(sortfunction);参数:sortFunction可选项.是用来确定元素顺序的函数的名称.如果这个参数被省略,那么元素将按照 ASCII 字符顺序进行升序排列.sort 方法将 Array 对象进行适当的排序:在执行过程中并不会创建新的 Array 对象.如果为 sortfunction 参数提供了一个函数,那么该函数必须返回下列值之一:负值,如果所传递的第一个参数比

廖雪峰js教程笔记4 sort排序的一些坑

排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个对象呢?直接比较数学上的大小是没有意义的,因此,比较的过程必须通过函数抽象出来.通常规定,对于两个元素x和y,如果认为x < y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法就不用关心具体的比较过程,而是根据比较结果直接排序. JavaScript的Array的sort()方法就是用于排序的,但是

sort方法的使用、随机数的产生

如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序. var arr = ['a','b','m','c','d']; arr.sort(); //alert(arr); //结果:a b c d m var arr2 = [0,2,3,91,10]; arr2.sort(); //alert(arr2); //结果:0 10 2 3 91 arr2.sort(function(a,b){ return a-b; }); //alert(

[leetcode 75] Sort Colors

1 题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, a

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o