quicksort

randomized quick sort in Java need a suble fix?

若如初见. 提交于 2019-12-24 16:16:41
问题 I have implemented the quicksort algorithm that uses the first element of the list as pivot and it worked fine. now I refactored to pick a random index as pivot element, swap with the first element and do the quicksort subroutine. somehow, it does not work, I do not get the sorted array. here is my code, which is self-explanatory, but I am happy to explain if any clarification needed. public class Qsort { public static void quickSort2(int[] arr, int i, int j){ if (i<j){ int part

Sorting a 2D array with qsort

时光怂恿深爱的人放手 提交于 2019-12-24 14:28:25
问题 I'm trying to sort 2d array. First i sort it by column, then by rows. Column by column is working but row by row not. What's wrong in this code? int scmpr (const void *a, const void *b){ return strcmp((const char*)a, (const char*)b); } int main(void){ int i,j; char **tab; tab=(char**)malloc(sizeof(char*)* 10); for(i=0; i<10; i++){ tab[i]=(char*)malloc(sizeof(char)*15); } for(i=0; i<10; i++){ for(j=0; j<15; j++){ tab[i][j]=rand()%20+'b'; printf("%c ", tab[i][j]); } puts(""); } for (i = 0; i<10

converting a recursion to iteration in python

不打扰是莪最后的温柔 提交于 2019-12-24 13:51:33
问题 I wrote the below python script that sorts the elements of an array using divide-and-conquer (recursive calls). One of my friend suggested that recursion is slower than iteration. Is there a way to convert the below program to a 'for' loop and still leverage divide-and-conquer strategy. Will iteration beat recursion even if the list contains a lot of elements? ### Using recursion import random from datetime import datetime start = str(datetime.now().time()).split(':') def quicksort(A,first

Sort list model with priority id's and date time

爱⌒轻易说出口 提交于 2019-12-24 12:27:24
问题 Sample List ArrayList<MyObject> list = new ArrayList<MyObject>(); list.add(new MyObject (1, "2011-04-27T09:40:01.607")); list.add(new MyObject (1, "2011-05-27T09:42:01.607")); list.add(new MyObject (2, "2011-06-27T09:42:01.607")); list.add(new MyObject (5, "2011-07-27T09:43:01.607")); list.add(new MyObject (6, "2011-08-27T09:44:01.607")); list.add(new MyObject (6, "2011-09-27T09:45:01.607")); list.add(new MyObject (1, "2011-10-27T09:46:01.607")); 1:-How to Sort ArrayList with the respect of

quicksort with linked lists

南楼画角 提交于 2019-12-24 10:26:38
问题 I have written down the following program that uses the quicksort algorithm to sort how ever many ints are put into the command line using linked lists. Not only am I getting an ISO C90 error about mixed declarations but there is a memory leak somewhere in my code and I am not sure how to fix it. Any help would be appreciated! #include <stdio.h> #include "linked_list.h" #include <stdlib.h> #include "memcheck.h" #include <string.h> #include <assert.h> node *quicksort(node *list); int

erlang - is it possible to output the list length after having it run a quicksort?

自闭症网瘾萝莉.ら 提交于 2019-12-24 07:38:28
问题 I'm a newbie to Erlang. I've been running a quick sort on a random list of numbers (i've also had it only keep unique numbers so duplicates do not show up in the sorted list). It works fine in that the output is giving the sorted numbers with no duplicates, but I have been trying to have it output not only the list, but also the length list too which is where I'm running into errors. The length(mod:func). will give the length of the list no problem in the erlang shell, but I can't get it to

Is there any formula to calculate the no of passes that a Quick Sort algorithm will take?

不问归期 提交于 2019-12-24 06:00:15
问题 While working with Quick Sort algorithm I wondered whether any formula or some kind of stuff might be available for finding the no of passes that a particular set of values may take to completely sorted in ascending order. Is there any formula to calculate the no of passes that a Quick Sort algorithm will take? 回答1: Any given set of values will have a different number of operations, based on pivot value selection method, and the actual values being sorted. So...no, unless the approximations

Is there any formula to calculate the no of passes that a Quick Sort algorithm will take?

匆匆过客 提交于 2019-12-24 05:59:10
问题 While working with Quick Sort algorithm I wondered whether any formula or some kind of stuff might be available for finding the no of passes that a particular set of values may take to completely sorted in ascending order. Is there any formula to calculate the no of passes that a Quick Sort algorithm will take? 回答1: Any given set of values will have a different number of operations, based on pivot value selection method, and the actual values being sorted. So...no, unless the approximations

understand the running trace of quicksort in prolog

荒凉一梦 提交于 2019-12-24 03:52:24
问题 I have the following 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). split(X,[],[],[]). split(X,[Y|Tail],[Y|Small],Big):- gt(X,Y), !, split(X,Tail,Small, Big). split(X,[Y|Tail],Small,[Y|Big]):- split(X,Tail,Small,Big). The array for

In-place partition when the array may or may not contain the pivot element

天涯浪子 提交于 2019-12-24 03:14:45
问题 Is there an in-place partitioning algorithm (of the kind used in a Quicksort implementation) that does not rely on the pivot element being present in the array? In other words, the array elements must be arranged in this order: Elements less than the pivot (if any) Elements equal to the pivot (if any) Elements greater than the pivot (if any) It must still return the index (after sorting) of the pivot element if it happens to be present in the array, or a special value if not; This could be