quicksort

Implementation of Quick Sort

妖精的绣舞 提交于 2019-12-12 04:35:55
问题 #include<stdio.h> #include<iostream.h> #include<conio.h> void quicks(int *arr,int x,int pivot,int lo,int hi); void swap1(int *x,int *y); int main() { int *arr = new int[7]; arr[0] = 23; arr[1] = 3; arr[2] = -23; arr[3] = 45; arr[4] = 12; arr[5] = 76; arr[6] = -65; quicks(arr,7,0,1,6); for(int i = 0;i<7;i++) std::cout << arr[i] <<"\t"; getch(); return 0; } void quicks(int *arr,int x,int pivot,int lo,int hi) { int i = lo,j = hi; if(pivot < x-1) { while(i <= hi) { if(arr[i] <= arr[pivot]) i++;

trace route of the quicksort algorithm - prolog

爱⌒轻易说出口 提交于 2019-12-12 01:56:50
问题 I have that previous question about this quicksort here.The prolog code for quicksort: gt(X,Y):- X @>Y. conc([],List, List). conc([Head|Tail], List1, [Head|List2]):- conc(Tail, List1, List2). quicksort([],[]). quicksort([X|Tail],Sorted):- split(X,Tail,Small,Big), quicksort(Small,SortedSmall), quicksort(Big,SortedBig), conc(SortedSmall,[X|SortedBig],Sorted). [1]split(X,[],[],[]). [2]split(X,[Y|Tail],[Y|Small],Big):- gt(X,Y),!, split(X,Tail,Small,Big). [3]split(X,[Y|Tail],Small,[Y|Big]):- split

java.lang.IllegalArgumentException in QuickSort method [closed]

无人久伴 提交于 2019-12-11 21:57:02
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I am following the following pseudocode: function quicksort(array) if length(array) > 1 pivot := select any element of array left := first index of array right := last index of array while left ≤ right while array[left] < pivot left := left + 1 while array[right] > pivot right := right - 1 if left ≤ right swap

Why QuickSort bad at sorting almost sorted data [closed]

删除回忆录丶 提交于 2019-12-11 17:56:18
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 12 months ago . Why is QuickSort bad at sorting almost sorted data? In comparison, why is insertion sort better? Trying to understand Big O notation! 回答1: Your statement is true for certain variants of QS depending on the choice of pivot. QS performance depends on the pivoting operation to

Some issues in implementing QuickSort in java

北慕城南 提交于 2019-12-11 17:03:59
问题 Here is my code: public class Main { public static void main(String[] args) { int[] temp = {4,2,6,4,5,2,9,7,11,0,-1,4,-5}; quickSort(temp); for(int s : temp) System.out.println(s); } public static void quickSort(int[] data) { quickSort(data, 0, data.length); } public static void quickSort(int[] data, int first, int n) { int p, n1, n2; if(n > 1) { p = partition(data, first, n); n1 = p - first; n2 = n - n1 - 1; quickSort(data, first, n1); quickSort(data, p+1, n2); } } private static int

QuickSort with Many Elements causes StackOverflowError

不打扰是莪最后的温柔 提交于 2019-12-11 16:21:25
问题 Good day! I am getting a StackOverflowError when running my quickSort algorithm. This error occurs when elements in the array > 50 000. My code is below : public void recQuickSort(int left, int right) { if(right-left <= 0) return; else { long pivot = a[right]; int partition = partitionIt(left, right, pivot); recQuickSort(left, partition-1); recQuickSort(partition+1, right); } } public int partitionIt(int left, int right, long pivot) { int leftPtr = left - 1; int rightPtr = right; while (true)

How to find K smallest values using quick sort

匆匆过客 提交于 2019-12-11 15:24:51
问题 The problem is simple if I sort all the values and pick the fist K values. But it wastes too much time because maybe the smallest K values has been sorted before the whole array is sorted. I think the way to solve this problem is to add a flag in the code, but I can not figure out how to put the flag to judge whether the smallest k values has been sort. 回答1: You can use random selection algorithm to solve this problem with O(n) time. In the end, just return sub-array from 0 to k. 回答2: I think

I want to quick sort using linked lists

為{幸葍}努か 提交于 2019-12-11 15:21:43
问题 Write a function that takes two student record structures by reference and swaps all their contents except their next pointers. Use your functions to implement bubble sort algorithm to sort the linked list (do not use arrays). #include <stdio.h> #include <stdlib.h> #include <string.h> /*defined a structure for date of birth*/ struct birth{ int date; int month; int year; }; struct studentrecord{ char name[64]; struct birth dob; int height; float weight; struct studentrecord *next; struct

Does returning “-1” with usort really move the $b variable or does it keep it in the same place?

允我心安 提交于 2019-12-11 14:16:38
问题 A simple piece of code written by me: <?php function testing($a,$b){ if ($a < $b ){ return -1; } elseif ($a > $b){ return 1; } //else { //return 0; //} } $array = array(1,3,2,4,5); usort($array, "testing"); var_dump($array); ?> This is from the top comment (highest rated comment and from 5 years ago) on the php.net manual's usort page: "If you return -1 that moves the $b variable down the array, return 1 moves $b up the array and return 0 keeps $b in the same place." As far as I was looking

c++ quicksort sort string text

百般思念 提交于 2019-12-11 13:11:53
问题 I am doing my homework, and completed quicksort recursive, however it doesn't sort in a correct way. Seems to be it doesn't swap correctly. Here is my code #include<iostream> #include<ctime> #include<string> using namespace std; int quick_sort_help(string &text,int left, int right, int pivot){ char val = text[pivot]; char temp; //swap // temp =text[pivot]; //text[pivot]= text[right]; //text[right]=temp; //swap(&text[left],&text[right]); int l = left; int r = right; int i=left; while (i<=r) {