0
\$\begingroup\$
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 ?

Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked May 15, 2020 at 23:00
\$\endgroup\$
2
  • \$\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\$ Commented 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\$ Commented May 16, 2020 at 5:23

1 Answer 1

3
\$\begingroup\$
  1. 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).
  2. In quicksort, you should not pass in a pivot. The function should work that out itself.
  3. 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
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.