quicksort

How can I fix the issue in quick sort algoritm in Java

孤者浪人 提交于 2020-06-17 09:43:26
问题 I have a problem about sorting object array thanks to quick sort algorithm. I created Person object including id , name , surname and lastly age . I used comparator to sort list in terms of the attributes of person object. Here is an example shown below. Comparator<Person> compTr = new Comparator<Person>() { @Override public int compare(Person p0, Person p1) { return Long.compare(p0.getId(), p1.getId()); } }; I think my problem is located in both two while loop by implementing greaterthan and

QuickSort implementation does not work for duplicate key

五迷三道 提交于 2020-04-30 06:49:25
问题 I tried to implement Quicksort. It works fine except when there is a duplicate key, in which case there is an infinite loop and it never terminates. Can you help me understand what I am doing wrong? // quick sort void quickSort(int arr[], const unsigned size) { // base case if (size < 2) return; int pivot = arr[size / 2]; unsigned L = 0, U = size - 1; // partitioning while (L < U) { while (arr[L] < pivot) L++; while (arr[U] > pivot) U--; swap(&arr[L], &arr[U]); } quickSort(arr, L); // sort

Quicksort with double* values

ε祈祈猫儿з 提交于 2020-04-22 04:09:04
问题 I am given a programme in C which implements Quicksort on arrays with int values. I need to convert it into a programme which will implement Quicksort on arrays with double* values. I thought that I just need to change "int" declarations into "double*", but for some reason the programme no longer works when I test arrays with other values than integers. Can someone please help? I really know almost nothing about programming in C and have no idea how to go on. Here is the "int" programme: void

Binary trees and quicksort?

左心房为你撑大大i 提交于 2020-04-16 05:56:28
问题 I have a homework assignment that reads as follows (don't flame/worry, I am not asking you to do my homework): Write a program that sorts a set of numbers by using the Quick Sort method using a binary search tree. The recommended implementation is to use a recursive algorithm. What does this mean? Here are my interpretations thus far, and as I explain below, I think both are flawed: A. Get an array of numbers (integers, or whatever) from the user. Quicksort them with the normal quicksort

QuickSort on a doubly linked list not working as it should

匆匆过客 提交于 2020-03-03 14:00:13
问题 I used an algorithm that I've used in the past for arrays. This always picks the first element as the pivot. Here's the code: void quickSort(int a[],int l,int r,int *count) { if(l<r-1) { *count=*count+r-l-1; int q=partition(a,l,r); //finding the pivot position in sorted array quickSort(a,l,q-1,count); //recursive calling before pivot sub array quickSort(a,q+1,r,count); //recursive calling after pivot sub array } } //partition function definition int partition(int a[],int l,int r) { int j,temp

使用Java泛型实现快速排序(快排,Quicksort)算法

巧了我就是萌 提交于 2020-02-28 21:54:53
快排算法的特点 实用性强。 很多实际的项目中使用了快排算法。但通常对算法都进行了调整(tuning),比如Java.util.Arrays类中的sort函数就使用了快排算法,但使用了双参考值( Dual-Pivot Quicksort )等一些改进措施。由于快排算法为递归算法,可以用循环代替递归函数调用,改进性能。 不需要额外的空间。 可以将数组中的数据直接交换位置实现排序,所以理论上不需要额外的空间。 时间复杂度 平均情况:O(nlgn) 最坏情况: O(n*n),发生在当数据已经是排序状态时 快排算法的基本原理 1、从数据中选取一个值a[i]作为参考 2、以a[i] 为参考,将数据分成2部分:P1、P2,P1中的数据全部≤a[i],P2中的数据全部>a[i],数据变为{{P1}{a[i]}{P2}} 3、将P1、P2重复上述步骤,直到各部分中只剩1个数据 4、数据完成升序排列 示例: 原始数据: {3,9,8,5,2,1,6} 第1步:选取第1个数据:3 第2步:将数据分成2部分,左边≤3,右边大于>3: {2,1} {3} {9,8,5,6} 第3步:将各部分重复以上步骤,直到每部分只剩1个数据: {2,1} => {1} {2} {9,8,5,6} => {8,5,6} {9}=> {5,6} {8} {9}=> {5} {6} {8} {9} 第4步:数据完成升序排列:

Sorting CSV file by column using QuickSort c#

£可爱£侵袭症+ 提交于 2020-02-16 11:31:13
问题 I have a file called Item.csv file which has the following information: categoryName, currentPrice, currencyId Boots, 19.95, GBP Thermometers,2.03,GBP Garden Sheds,38.95,GBP I want to sort the content by price by making use of QSortAlgorithm and save it as sortedItem.csv. So far I can pull out the price column and sort it by making use of QSortAlgorithm but I don't know how to put it all together. Any help would be highly appreciated. List <double> priceList=new List<double>(); using

Sorting CSV file by column using QuickSort c#

让人想犯罪 __ 提交于 2020-02-16 11:30:42
问题 I have a file called Item.csv file which has the following information: categoryName, currentPrice, currencyId Boots, 19.95, GBP Thermometers,2.03,GBP Garden Sheds,38.95,GBP I want to sort the content by price by making use of QSortAlgorithm and save it as sortedItem.csv. So far I can pull out the price column and sort it by making use of QSortAlgorithm but I don't know how to put it all together. Any help would be highly appreciated. List <double> priceList=new List<double>(); using

Sorting multiple arrays c#

戏子无情 提交于 2020-01-25 11:15:51
问题 currently I'm working on a project where i'm required to sort a total of 6 arrays. I've managed to sort the arrays individually using a quicksort, however, I'm just wondering if there is a way to sort 1 array and reflect that on the order of elements in the other arrays. For example, if I sort my date array to an ascending order, I want the other arrays to still match up with the dates in respect to the new order. If possible, could this still be done through a quick sort? 回答1: Likely you

Benchmarking quicksort and mergesort yields that mergesort is faster

醉酒当歌 提交于 2020-01-24 13:57:05
问题 I've tried benchmarking and for some reason when trying both of them on array of 1M elements the Mergesort sorted it in 0.3s and Quicksort took 1.3s. I've heard that generally quicksort is faster, because of its memory management, but how would one explain these results? I am running MacBook Pro if that makes any difference. The input is a set of randomly generated integers from 0 to 127. The codes are in Java: MergeSort: static void mergesort(int arr[]) { int n = arr.length; if (n < 2)