Forum » Programiranje » Quick sort ascending/descending
Quick sort ascending/descending
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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?![:8)](https://static.slo-tech.com/smeski/icon_redface.gif)
Tale algoritem sortira od najmanjšega do največjega. Kako ga spremeniti, da bo sortiral od največjega do najmanjšega?
![:8)](https://static.slo-tech.com/smeski/icon_redface.gif)
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 ()
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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 ()
![](https://static.slo-tech.com/stili/avatar_gray.gif)
Thomas ::
Moraš zamenjati komparatorje "večji" in "manjši" - pa mora delat!
Man muss immer generalisieren - Carl Jacobi
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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);
![](https://static.slo-tech.com/stili/bel_rudis.png)
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))
![](https://static.slo-tech.com/stili/avatar_gray.gif)
Thomas ::
Če boš komparatorje naredil prav (obratno!) - bo delalo, sicer ne.
Man muss immer generalisieren - Carl Jacobi
![](https://static.slo-tech.com/stili/bel_rudis.png)
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; } }
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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. :(
![](https://static.slo-tech.com/stili/avatar_gray.gif)
Thomas ::
Ti mene posluš!
Samo tam kjer PRIMERJAŠ, primerjave obrni.
Samo tam kjer PRIMERJAŠ, primerjave obrni.
Man muss immer generalisieren - Carl Jacobi
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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
![](https://static.slo-tech.com/stili/avatar_gray.gif)
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.
![;)](https://static.slo-tech.com/smeski/icon_wink.gif)
![:\](https://static.slo-tech.com/smeski/icon_rolleyes.gif)
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 | 3198 (1928) | peterv6i |
» | Java - sortiranjeOddelek: Programiranje | 1181 (967) | rrejc |
» | [Java] QuicksortOddelek: Programiranje | 758 (594) | MrBrdo |
» | [JavaScript] Sortiranje šumnikovOddelek: Programiranje | 2180 (1914) | MarkookraM |
» | [Turbo Pascal] Pomoč...Oddelek: Programiranje | 1494 (1396) | Grey |