Java脚本和 Java 中的快速排序
Quicksort in JavaScript and Java
刚刚发现一个奇怪的问题。我尝试使用Java脚本实现基于Java中编写的代码的快速排序,然而,结果不是我所期望的。
Java代码:
public class Sort {
public static void main (String[] args){
int[] number = {1,10,2,2,2,3,5,7,8,2};
for (int i = 0; i < number.length ; i++) {
System.out.println(quickSort(number)[i]);//1,2,2,2,2,3,5,7,8,10
}
}
static int[] quickSort(int[] number){
int length = number.length;
int start = 0;
int end = length -1;
number = quick_sort (start,end,number);
return number;
}
private static int[] quick_sort(int start,int end,int[] number) {
int pivot = number[(start+end)/2];
int s = start;
int e = end;
while (s <= e) {
while(number[s] < pivot) {
s++;
}
while(number[e] > pivot) {
e--;
}
//swap the value
if (s <= e) {
int temp = number[s];
number[s] = number[e];
number[e] = temp;
//move both sides cursor
s++;
e--;
}
}
// recursively sorting lower half
if (start < e) {
quick_sort(start, e, number);
}
//recursively sorting higher half
if (end > s) {
quick_sort(s, end, number);
}
return number;
}
}
Java脚本:
function main() {
var numbers = [1,10,2,2,2,3,5,7,8,2];
var length = numbers.length;
var start = 0;
var end = length - 1;
alert(quickSort(start, end, numbers));//2,3,5,8,7,2,1,2,2,10 <----- not sorted
}
function quickSort(start, end, numbers) {
var s,e,temp,pivot;
s = start;
e = end;
pivot = numbers[(start+end)/2];
while(s<=e){
while(numbers[s] < pivot ) {
s++;
}
while(numbers[e] > pivot) {
e--;
}
if(s<=e) {
temp = numbers[s];
numbers[s] = numbers[e];
numbers[e] = temp;
e--;
s++;
}
}
if(start < e) {
quickSort(start, e, numbers);
}
if(end > s) {
quickSort(s, end, numbers);
}
return numbers;
}
main();
写在Java中的代码给出了1,2,2,2,2,3,5,7,8,10的结果,在另一个hard中,写在Java中的代码脚本生成 2,3,5,8,7,2,1,2,2,10 的结果,这与原始数组不同,但仍未正确排序。对此有什么想法吗?提前致谢
编辑:
感谢您的回复,我正在研究逻辑而不是基础知识。
(start+end)/2
在 JS 中不一定是整数。它适用于 Math.floor((start+end)/2)
.
刚刚发现一个奇怪的问题。我尝试使用Java脚本实现基于Java中编写的代码的快速排序,然而,结果不是我所期望的。 Java代码:
public class Sort {
public static void main (String[] args){
int[] number = {1,10,2,2,2,3,5,7,8,2};
for (int i = 0; i < number.length ; i++) {
System.out.println(quickSort(number)[i]);//1,2,2,2,2,3,5,7,8,10
}
}
static int[] quickSort(int[] number){
int length = number.length;
int start = 0;
int end = length -1;
number = quick_sort (start,end,number);
return number;
}
private static int[] quick_sort(int start,int end,int[] number) {
int pivot = number[(start+end)/2];
int s = start;
int e = end;
while (s <= e) {
while(number[s] < pivot) {
s++;
}
while(number[e] > pivot) {
e--;
}
//swap the value
if (s <= e) {
int temp = number[s];
number[s] = number[e];
number[e] = temp;
//move both sides cursor
s++;
e--;
}
}
// recursively sorting lower half
if (start < e) {
quick_sort(start, e, number);
}
//recursively sorting higher half
if (end > s) {
quick_sort(s, end, number);
}
return number;
}
}
Java脚本:
function main() {
var numbers = [1,10,2,2,2,3,5,7,8,2];
var length = numbers.length;
var start = 0;
var end = length - 1;
alert(quickSort(start, end, numbers));//2,3,5,8,7,2,1,2,2,10 <----- not sorted
}
function quickSort(start, end, numbers) {
var s,e,temp,pivot;
s = start;
e = end;
pivot = numbers[(start+end)/2];
while(s<=e){
while(numbers[s] < pivot ) {
s++;
}
while(numbers[e] > pivot) {
e--;
}
if(s<=e) {
temp = numbers[s];
numbers[s] = numbers[e];
numbers[e] = temp;
e--;
s++;
}
}
if(start < e) {
quickSort(start, e, numbers);
}
if(end > s) {
quickSort(s, end, numbers);
}
return numbers;
}
main();
写在Java中的代码给出了1,2,2,2,2,3,5,7,8,10的结果,在另一个hard中,写在Java中的代码脚本生成 2,3,5,8,7,2,1,2,2,10 的结果,这与原始数组不同,但仍未正确排序。对此有什么想法吗?提前致谢
编辑:
感谢您的回复,我正在研究逻辑而不是基础知识。
(start+end)/2
在 JS 中不一定是整数。它适用于 Math.floor((start+end)/2)
.