<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="de">
	<id>https://wiki-de.moshellshocker.dns64.de/index.php?action=history&amp;feed=atom&amp;title=Swap-Sort</id>
	<title>Swap-Sort - Versionsgeschichte</title>
	<link rel="self" type="application/atom+xml" href="https://wiki-de.moshellshocker.dns64.de/index.php?action=history&amp;feed=atom&amp;title=Swap-Sort"/>
	<link rel="alternate" type="text/html" href="https://wiki-de.moshellshocker.dns64.de/index.php?title=Swap-Sort&amp;action=history"/>
	<updated>2026-05-22T09:54:02Z</updated>
	<subtitle>Versionsgeschichte dieser Seite in Wikipedia (Deutsch) – Lokale Kopie</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://wiki-de.moshellshocker.dns64.de/index.php?title=Swap-Sort&amp;diff=701903&amp;oldid=prev</id>
		<title>~2025-40783-41: /* Ada */</title>
		<link rel="alternate" type="text/html" href="https://wiki-de.moshellshocker.dns64.de/index.php?title=Swap-Sort&amp;diff=701903&amp;oldid=prev"/>
		<updated>2025-12-15T08:34:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Ada&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Neue Seite&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;Swap-Sort&amp;#039;&amp;#039;&amp;#039; ist ein [[Sortieralgorithmus]], der ein [[Feld (Datentyp)|Array]] aus paarweise verschiedenen Zahlen sortiert.&lt;br /&gt;
&lt;br /&gt;
== Idee ==&lt;br /&gt;
&lt;br /&gt;
Die Idee von Swap-Sort ist, von jedem Element eines Arrays &amp;#039;&amp;#039;A(1…n)&amp;#039;&amp;#039; die Anzahl &amp;#039;&amp;#039;m&amp;#039;&amp;#039; der kleineren Werte (die in &amp;#039;&amp;#039;A&amp;#039;&amp;#039; sind) zu zählen und das Element dann mit dem Element in &amp;#039;&amp;#039;A(m+1)&amp;#039;&amp;#039; zu vertauschen. Somit ist sichergestellt, dass das ausgetauschte Element bereits an der richtigen, also endgültigen Stelle steht.&lt;br /&gt;
&lt;br /&gt;
Nachteil dieses [[Algorithmus]] ist, dass jedes Element nur einmal vorkommen darf, da sonst keine [[Terminiertheit|Terminierung]] erfolgt.&lt;br /&gt;
&lt;br /&gt;
== Prinzip ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;A&amp;#039;&amp;#039; ist ein Array mit &amp;#039;&amp;#039;n&amp;#039;&amp;#039; Elementen. Swap-Sort arbeitet in folgenden Schritten:&lt;br /&gt;
# Beginne mit &amp;lt;code&amp;gt;i = 1&amp;lt;/code&amp;gt;&lt;br /&gt;
# Zähle, wie viele Elemente kleiner als &amp;#039;&amp;#039;A(i)&amp;#039;&amp;#039; sind, m sei diese Anzahl. Tausche danach &amp;#039;&amp;#039;A(i)&amp;#039;&amp;#039; mit &amp;#039;&amp;#039;A(m+1)&amp;#039;&amp;#039;&lt;br /&gt;
# Ist &amp;lt;code&amp;gt;i = m+1&amp;lt;/code&amp;gt;, so erhöhe &amp;#039;&amp;#039;i&amp;#039;&amp;#039; um 1&lt;br /&gt;
# Ist &amp;lt;code&amp;gt;i = n&amp;lt;/code&amp;gt;, so ist die Sortierung beendet. Andernfalls gehe wieder zu Schritt&amp;amp;nbsp;2.&lt;br /&gt;
&lt;br /&gt;
== Beispiel ==&lt;br /&gt;
&lt;br /&gt;
Es soll ein Array mit dem Inhalt {7,8,5,2,4,9,3,1} sortiert werden.&lt;br /&gt;
&lt;br /&gt;
 7 8 5 2 4 9 3 1   Mit dem Index 1 wird begonnen&lt;br /&gt;
 ^&lt;br /&gt;
 9 8 5 2 4 7 3 1   7 wurde mit A(5+1) getauscht&lt;br /&gt;
 ^&lt;br /&gt;
 1 8 5 2 4 7 3 9   9 wurde mit A(7+1) getauscht&lt;br /&gt;
 ^&lt;br /&gt;
 1 8 5 2 4 7 3 9   der Index wurde erhöht, da die Anzahl m der&lt;br /&gt;
   ^               kleineren Elemente von A(1) gleich dem Index-1 war&lt;br /&gt;
 1 3 5 2 4 7 8 9   8 wurde mit A(6+1) getauscht&lt;br /&gt;
   ^&lt;br /&gt;
 1 5 3 2 4 7 8 9&lt;br /&gt;
   ^&lt;br /&gt;
 1 4 3 2 5 7 8 9&lt;br /&gt;
   ^&lt;br /&gt;
 1 2 3 4 5 7 8 9&lt;br /&gt;
   ^  &lt;br /&gt;
 1 2 3 4 5 7 8 9   der Index wurde erhöht, da die Anzahl m der&lt;br /&gt;
     ^             kleineren Elemente von A(2) gleich dem Index-1 war&lt;br /&gt;
 1 2 3 4 5 7 8 9   ...usw.&lt;br /&gt;
       ^&lt;br /&gt;
 1 2 3 4 5 7 8 9&lt;br /&gt;
         ^&lt;br /&gt;
 1 2 3 4 5 7 8 9&lt;br /&gt;
           ^&lt;br /&gt;
 1 2 3 4 5 7 8 9&lt;br /&gt;
             ^&lt;br /&gt;
 1 2 3 4 5 7 8 9  Das Array wurde komplett durchlaufen,&lt;br /&gt;
               ^  das Sortieren ist somit beendet&lt;br /&gt;
&lt;br /&gt;
== Komplexität ==&lt;br /&gt;
&lt;br /&gt;
Für die Bestimmung der Anzahl kleinerer Einträge ist ein vollständiger Array-Durchlauf nötig. Ein solcher muss für jedes Element des Arrays durchgeführt werden. Es ergibt sich eine Komplexität von &amp;lt;math&amp;gt;\mathcal O(n^2)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Implementierung ==&lt;br /&gt;
=== Ada ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ada&amp;quot;&amp;gt;&lt;br /&gt;
procedure swapsort (a: in out vector) is&lt;br /&gt;
&lt;br /&gt;
z:integer;&lt;br /&gt;
&lt;br /&gt;
	function ziel_pos(z:integer) return natural is&lt;br /&gt;
		anz:natural:=0;&lt;br /&gt;
	begin&lt;br /&gt;
		for i in a&amp;#039;range loop&lt;br /&gt;
			if a(i) &amp;lt;= z then&lt;br /&gt;
				anz:=anz+1;&lt;br /&gt;
			end if;&lt;br /&gt;
		end loop;&lt;br /&gt;
		return anz;&lt;br /&gt;
	end;&lt;br /&gt;
&lt;br /&gt;
begin&lt;br /&gt;
	for index in a&amp;#039;range loop&lt;br /&gt;
		z:=ziel_pos(a(index));&lt;br /&gt;
		while z /= index loop&lt;br /&gt;
			&amp;lt; vertausche a(index) mit a(z) &amp;gt;;&lt;br /&gt;
			z:=ziel_pos(a(index));&lt;br /&gt;
		end loop;&lt;br /&gt;
	end loop;&lt;br /&gt;
end;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Haskell ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Haskell&amp;quot;&amp;gt;&lt;br /&gt;
swapSort x = swapSort&amp;#039; x 0 where&lt;br /&gt;
&lt;br /&gt;
  swapSort&amp;#039; x i | i == length x = x&lt;br /&gt;
                | i == smaller  = swapSort&amp;#039; x (i+1)&lt;br /&gt;
                | otherwise     = swapSort&amp;#039; (swap x i smaller) i where&lt;br /&gt;
                    smaller = countSmaller x (x !! i)&lt;br /&gt;
&lt;br /&gt;
  countSmaller = countSmaller&amp;#039; 0&lt;br /&gt;
  countSmaller n []     _ = n&lt;br /&gt;
  countSmaller n (x:xs) y | x &amp;lt; y     = countSmaller (n+1) xs y&lt;br /&gt;
                          | otherwise = countSmaller  n    xs y&lt;br /&gt;
&lt;br /&gt;
  swap x i1 i2 = insert (remove (insert (remove x i1) e1 (i2-1)) i2) e2 i1 where&lt;br /&gt;
    e1 = x !! i1&lt;br /&gt;
    e2 = x !! i2&lt;br /&gt;
&lt;br /&gt;
  remove (x:xs) 0 = xs&lt;br /&gt;
  remove (x:xs) n = x : remove xs (n-1)&lt;br /&gt;
  remove []     _ = error &amp;quot;Index zu groß&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  insert x      y 0 = y:x&lt;br /&gt;
  insert (x:xs) y n = x:insert xs y (n-1)&lt;br /&gt;
  insert []     _ _ = error &amp;quot;Index zu groß&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Java ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class SwapSorter {&lt;br /&gt;
&lt;br /&gt;
    public void sort(int[] sortMe) {&lt;br /&gt;
&lt;br /&gt;
        int startwert = 0;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        while (startwert &amp;lt; sortMe.length - 1) {&lt;br /&gt;
&lt;br /&gt;
            int kleinere = countSmallerOnes(sortMe, startwert);&lt;br /&gt;
&lt;br /&gt;
            if (kleinere &amp;gt; 0) {&lt;br /&gt;
		int tmp = sortMe[startwert];&lt;br /&gt;
                sortMe[startwert] = sortMe[startwert + kleinere];&lt;br /&gt;
                sortMe[startwert + kleinere] = tmp;&lt;br /&gt;
            }&lt;br /&gt;
            else&lt;br /&gt;
            {&lt;br /&gt;
                startwert++;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    private int countSmallerOnes(final int[] countHere, final int index) {&lt;br /&gt;
        int counter = 0;&lt;br /&gt;
        for (int i = index + 1; i &amp;lt; countHere.length; i++) {&lt;br /&gt;
            if (countHere[index] &amp;gt; countHere[i]) {&lt;br /&gt;
                counter++;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return counter;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Testmain&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
&lt;br /&gt;
        int[] a = {3, 7, 45, 1, 33, 5, 2, 9};&lt;br /&gt;
        System.out.print(&amp;quot;unsorted: &amp;quot;);&lt;br /&gt;
        for (int i = 0; i &amp;lt; a.length; i++) {&lt;br /&gt;
            System.out.print(a[i] + &amp;quot;  &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        System.out.println();&lt;br /&gt;
        new SwapSorter().sort(a);&lt;br /&gt;
        System.out.print(&amp;quot;sorted: &amp;quot;);&lt;br /&gt;
        for (int i = 0; i &amp;lt; a.length; i++) {&lt;br /&gt;
            System.out.print(a[i] + &amp;quot;  &amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def swap_sort(sortme):&lt;br /&gt;
    index = 0&lt;br /&gt;
    while index &amp;lt; len(sortme) - 1:&lt;br /&gt;
        new_index = sum(x &amp;lt; sortme[index] for x in sortme)&lt;br /&gt;
        if index == new_index:&lt;br /&gt;
            index += 1&lt;br /&gt;
        else:&lt;br /&gt;
            sortme[index], sortme[new_index] = sortme[new_index], sortme[index]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Siehe auch ==&lt;br /&gt;
* [[Liste von Algorithmen]]&lt;br /&gt;
&lt;br /&gt;
[[Kategorie:Sortieralgorithmus]]&lt;/div&gt;</summary>
		<author><name>~2025-40783-41</name></author>
	</entry>
</feed>