» »

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? :8)

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;
}

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
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

PaX_MaN ::

Kot je KaRkY zapisal. Glej še, da pravi Collator not vržeš.

MarkookraM ::

Ne, tako tudi ne dela. :(

PaX_MaN ::

Sej pravim, ta pravi Collator mu podaj: http://java.sun.com/j2se/1.4.2/docs/api...

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);

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))

MarkookraM ::

Sem probal tak, pa ne dela. Pa po moji logiki in razumevanju algoritma bi moralo. 8-O

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. :(

Thomas ::

Ti mene posluš!

Samo tam kjer PRIMERJAŠ, primerjave obrni.
Man muss immer generalisieren - Carl Jacobi

MarkookraM ::

Ti pa mene posluš! :P

Sem probal te primerjave obrnit, pa ne dela.

Thomas ::

More delat! Zamenjaš takrat in samo takrat, ko PRIMERJAŠ vrednosti v polju. Ne, ko recimo šteješ in tko naprej.

Jasno?
Man muss immer generalisieren - Carl Jacobi

MarkookraM ::

Meni je jasno moji Java Virtual Machine pa zgleda ni. :D

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 ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[JAVA] HTTPS client

Oddelek: Programiranje
173174 (1904) peterv6i
»

Java - sortiranje

Oddelek: Programiranje
81159 (945) rrejc
»

[Java] Quicksort

Oddelek: Programiranje
6732 (568) MrBrdo
»

[JavaScript] Sortiranje šumnikov

Oddelek: Programiranje
152145 (1879) MarkookraM
»

[Turbo Pascal] Pomoč...

Oddelek: Programiranje
131468 (1370) Grey

Več podobnih tem