\$\begingroup\$
\$\endgroup\$
2
package labodeva;
import java.util.Scanner;
public class odev5dene {
static int[] quick(int kucuk,int buyuk,int[] arr,int pivot) {
int bos[] = new int[100];
int t=0;
while(kucuk<=buyuk) { // buyuk is big kucuk is small
if(arr[kucuk]<=arr[pivot]) {
kucuk++;
}
else if(arr[buyuk]>arr[pivot]) {
buyuk--;
}
else{
bos[0] = arr[kucuk];
arr[kucuk]=arr[buyuk];
arr[buyuk] = bos[0];
buyuk--;
kucuk++;
}
}
if(kucuk>buyuk) {
bos[0] = arr[pivot];
arr[pivot]=arr[buyuk];
arr[buyuk]=bos[0];
pivot=buyuk; //sil
}
if(pivot-1>0&&pivot+1<arr.length) {
quick(0,pivot-1,arr,0);
quick(pivot+1,arr.length-1,arr,pivot+1);
}
if(pivot+1<arr.length&&pivot-1<0) {
quick(pivot+1,arr.length-1,arr,pivot+1);
}
return arr;
}
public static void main(String[] args) {
int a =0;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter how many value do you want to add the array : ");
a=scan.nextInt();
int k[] =new int[a];
for(int i=0;i<a;i++) {
System.out.println("Please enter your "+(i+1)+".value");
k[i]=scan.nextInt();
}
k=quick(0,a-1,k,0); // it starts 0. element as pivot and goes on and also small and equal number value starts here big number value starts at the end of list
for(int i=0;i<a;i++) {
System.out.println(k[i]);
}
}
}
i wrote this code and it sorts the given array but i tried to do quicksort is this quicksort i am not sure can you check ?
-
\$\begingroup\$ (I always found it slightly more taxing to follow source code with comments&identifiers in my first language, and keywords in English as compared to sticking to (Pidgin) English.) \$\endgroup\$greybeard– greybeard2020年05月16日 05:18:14 +00:00Commented May 16, 2020 at 5:18
-
\$\begingroup\$ I expect a production strength quicksort implementation to a) use additional space growing not nearly as fast as the number n of items even worst case b) not show worst case time for reverse sorted input, let alone sorted one. \$\endgroup\$greybeard– greybeard2020年05月16日 05:23:47 +00:00Commented May 16, 2020 at 5:23
1 Answer 1
\$\begingroup\$
\$\endgroup\$
- In quicksort, when you are sorting from elements 20 to 40, and the split becomes 30, the recursive calls should be to sort 20 to 29 and 31 to 40. You did 0 to 29 and 31 to max. I suspect this made your code degenerate into an \$O(n^2)\$ sort (but still work).
- In quicksort, you should not pass in a pivot. The function should work that out itself.
- Variable bos is a problem. You only ever use bos[0], so you shouldn't even make an array, just a simple variable.
answered May 16, 2020 at 0:03
lang-java