Forum » Programiranje » Quick sort ascending/descending
Quick sort ascending/descending

MarkookraM ::
Ne me tepst ampak nekako ne znam obrniti vrstnega reda quick sorta.
Tale algoritem sortira od najmanjšega do največjega. Kako ga spremeniti, da bo sortiral od največjega do najmanjšega?
Tale algoritem sortira od najmanjšega do največjega. Kako ga spremeniti, da bo sortiral od največjega do najmanjšega?

public String[] quickSort(String[] record, int begin, int end, Collator Collator) { String[] tempek = new String[1]; int i = begin; int j = end; String mid; if (end > begin) { // Arbitrarily establishing partition element as the midpoint of the array. mid = record[(begin + end) / 2]; // loop through the array until indices cross while (i <= j) { // find the first element that is greater than or equal to the partition element starting from the left Index. while ((i < end) && (Collator.compare(record[i], mid) < 0)) // maybe here for descending? but it doesn't work :( { i++; } // find an element that is smaller than or equal to the partition element starting from the right Index. while ((j > begin) && (Collator.compare(record[j], mid) > 0)) // maybe here for descending? but it doesn't work :( { j--; } // if the indexes have not crossed, swap if (i <= j) { tempek[0] = record[j]; record[j] = record[i]; record[i] = tempek[0]; i++; j--; } } // If the right index has not reached the left side of array must now sort the left partition. if (begin < j) quickSort(record, begin, j, Collator); // If the left index has not reached the right side of array must now sort the right partition. if (i < end) quickSort(record, i, end, Collator); } return record_delo; }
- spremenil: MarkookraM ()

KaRkY ::
while ((i < end) && (Collator.compare(record[i], mid) > 0)) // maybe here for descending? but it doesn't work :(
{ i++; }
// find an element that is smaller than or equal to the partition element starting from the right Index.
while ((j > begin) && (Collator.compare(record[j], mid) < 0)) // maybe here for descending? but it doesn't work :(
{ j--; }
mislim da je to to nisem pa 100% že dolgo se nisem s tem ubadal
{ i++; }
// find an element that is smaller than or equal to the partition element starting from the right Index.
while ((j > begin) && (Collator.compare(record[j], mid) < 0)) // maybe here for descending? but it doesn't work :(
{ j--; }
mislim da je to to nisem pa 100% že dolgo se nisem s tem ubadal
When you look long into an abyss, the abyss looks into you
Zgodovina sprememb…
- spremenil: KaRkY ()

Thomas ::
Moraš zamenjati komparatorje "večji" in "manjši" - pa mora delat!
Man muss immer generalisieren - Carl Jacobi

MarkookraM ::
Če pa ascending algoritem deluje.
Tega dobi, ampak ni tukaj težava. Jaz ne znam algoritma obrnit da bi sortiral descending.
Locale localeSI = new Locale("sl", "SI");
Collator CollatorSI = Collator.getInstance(localeSI);
CollatorSI.setStrength(Collator.TERTIARY);
Tega dobi, ampak ni tukaj težava. Jaz ne znam algoritma obrnit da bi sortiral descending.
Locale localeSI = new Locale("sl", "SI");
Collator CollatorSI = Collator.getInstance(localeSI);
CollatorSI.setStrength(Collator.TERTIARY);

PaX_MaN ::
while ((i < end) && (Collator.compare(record[i], mid) < 0))
spremeni v
while ((i < end) && (Collator.compare(record[i], mid) > 0))
,
while ((j > begin) && (Collator.compare(record[j], mid) > 0))
pa v
while ((j > begin) && (Collator.compare(record[j], mid) < 0))
spremeni v
while ((i < end) && (Collator.compare(record[i], mid) > 0))
,
while ((j > begin) && (Collator.compare(record[j], mid) > 0))
pa v
while ((j > begin) && (Collator.compare(record[j], mid) < 0))

Thomas ::
Če boš komparatorje naredil prav (obratno!) - bo delalo, sicer ne.
Man muss immer generalisieren - Carl Jacobi

PaX_MaN ::
Jaz imam tole in deluje:
import java.text.Collator; import java.text.RuleBasedCollator; import java.util.Locale; public class blbl { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] b2 = {"aaa","addd","aff", "afde" , "aabe" , "aaee" , "aeef" ,"agge", "aa","a"}; RuleBasedCollator en_USCollator = (RuleBasedCollator) Collator.getInstance(new Locale("en", "US", "")); String[] o = blbl.quickSort(b2, 0,b2.length-1,Collator.getInstance()); for (int i = 0; i < o.length; i++) { System.out.println(o[i]); } } public static String[] quickSort(String[] record, int begin, int end, Collator Collator) { String[] tempek = new String[1]; int i = begin; int j = end; String mid; if (end > begin) { // Arbitrarily establishing partition element as the midpoint of the array. mid = record[(begin + end) / 2]; // loop through the array until indices cross while (i <= j) { // find the first element that is greater than or equal to the partition element starting from the left Index. while ((i < end) && (Collator.compare(record[i], mid) > 0)) // maybe here for descending? but it doesn't work :( { i++; } // find an element that is smaller than or equal to the partition element starting from the right Index. while ((j > begin) && (Collator.compare(record[j], mid) < 0)) // maybe here for descending? but it doesn't work :( { j--; } // if the indexes have not crossed, swap if (i <= j) { tempek[0] = record[j]; record[j] = record[i]; record[i] = tempek[0]; i++; j--; } } // If the right index has not reached the left side of array must now sort the left partition. if (begin < j) quickSort(record, begin, j, Collator); // If the left index has not reached the right side of array must now sort the right partition. if (i < end) quickSort(record, i, end, Collator); } return record; } }

MarkookraM ::
Tale tvoj algoritem mi sortira od najmanjšega do največjega prvo in drugo polovico recorda posebej. (a, b, c, d, a, b, c, d)
Jaz biti totalno zmeden. :(
Jaz biti totalno zmeden. :(

Thomas ::
Ti mene posluš!
Samo tam kjer PRIMERJAŠ, primerjave obrni.
Samo tam kjer PRIMERJAŠ, primerjave obrni.
Man muss immer generalisieren - Carl Jacobi

Thomas ::
More delat! Zamenjaš takrat in samo takrat, ko PRIMERJAŠ vrednosti v polju. Ne, ko recimo šteješ in tko naprej.
Jasno?
Jasno?
Man muss immer generalisieren - Carl Jacobi

infiniteLoop ::
JVM-u je vse jasno - dela tocno tiste bucke ki si jih ti naklofal (99.999% casa
). Ce menis, da si nasel bug v JVM-u
ga vsekakor cimprej prijavi.


None of us is as dumb as all of us.
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [JAVA] HTTPS clientOddelek: Programiranje | 3201 (1931) | peterv6i |
» | Java - sortiranjeOddelek: Programiranje | 1182 (968) | rrejc |
» | [Java] QuicksortOddelek: Programiranje | 761 (597) | MrBrdo |
» | [JavaScript] Sortiranje šumnikovOddelek: Programiranje | 2183 (1917) | MarkookraM |
» | [Turbo Pascal] Pomoč...Oddelek: Programiranje | 1495 (1397) | Grey |