<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Three Quadratic Sorts</title>
	<atom:link href="http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Vikas Tandi</title>
		<link>http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/#comment-2928</link>
		<dc:creator><![CDATA[Vikas Tandi]]></dc:creator>
		<pubDate>Wed, 27 Apr 2011 08:17:23 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1538#comment-2928</guid>
		<description><![CDATA[My c version
[sourcecode lang=&quot;cpp&quot;]
void straight_insertion_sort(int arr[], int left, int right)
{
	int i;

	/* move smallest key to left end */
	for(i = left+1; i &lt;= right; i++)
		if(arr[0] &gt; arr[i])
			swap(arr, 0, i);

	for(i = left+1; i &lt;= right; i++)
	{
		int j, key;

		for(j = i-1, key = arr[i]; arr[j] &gt; key; j--)
				arr[j+1] = arr[j];

		arr[j+1] = key;
	}
}

void bubble_sort(int arr[], int left, int right)
{
	int i, j;

	for(i = right; i &gt; left; i--)
	{
		for(j = left; j &lt; i; j++)
			if(arr[j] &gt; arr[j+1])
				swap(arr, j, j+1);
	}
}

void straight_selection_sort(int arr[], int left, int right)
{
	int i, j, min;

	for(i = left; i &lt; right; i++)
	{
		min = i;
		for(j = i+1; j &lt;= right; j++)
		{
			if(arr[j] &lt; arr[min])
				min = j;
		}
		swap(arr, i, min);
	}
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My c version</p>
<pre class="brush: cpp;">
void straight_insertion_sort(int arr[], int left, int right)
{
	int i;

	/* move smallest key to left end */
	for(i = left+1; i &lt;= right; i++)
		if(arr[0] &gt; arr[i])
			swap(arr, 0, i);

	for(i = left+1; i &lt;= right; i++)
	{
		int j, key;

		for(j = i-1, key = arr[i]; arr[j] &gt; key; j--)
				arr[j+1] = arr[j];

		arr[j+1] = key;
	}
}

void bubble_sort(int arr[], int left, int right)
{
	int i, j;

	for(i = right; i &gt; left; i--)
	{
		for(j = left; j &lt; i; j++)
			if(arr[j] &gt; arr[j+1])
				swap(arr, j, j+1);
	}
}

void straight_selection_sort(int arr[], int left, int right)
{
	int i, j, min;

	for(i = left; i &lt; right; i++)
	{
		min = i;
		for(j = i+1; j &lt;= right; j++)
		{
			if(arr[j] &lt; arr[min])
				min = j;
		}
		swap(arr, i, min);
	}
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/#comment-1579</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Fri, 13 Aug 2010 16:37:19 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1538#comment-1579</guid>
		<description><![CDATA[I&#039;ve done the C versions a long time ago, and I just revisited this exercise while teaching myself python. Thought I&#039;d post these here.
[sourcecode lang=&quot;python&quot;]
def bsort(tab):
    for i in range(len(tab) - 1): 
        for j in range(i + 1, len(tab)):
            if tab[j] &lt; tab[i]:
                tab[i], tab[j] = tab[j], tab[i]

def ssort(tab):
    for i in range(len(tab) - 1): 
        min = tab[i]
        indexmin = i 
        for j in range(i + 1, len(tab)):
            if tab[j] &lt; min:
                min = tab[j]
                indexmin = j 
        if indexmin != i:
            tab[i], tab[indexmin] = tab[indexmin], tab[i]

def isort(tab):
    for i in range(1, len(tab)):
        buffer = tab[i]
        j = i - 1 
        while j &gt;= 0 and tab[j] &gt; buffer:
            tab[j + 1] = tab[j]
            j -= 1
        tab[j + 1] = buffer
[/sourcecode]
and the C versions:
[sourcecode lang=&quot;cpp&quot;]
int insert_sort(int *tab, int l)
{
    int *ip, *jp;
    int value;
    printf(&quot;insert sorting!\n&quot;);
    for (ip = tab + 1; ip &lt; tab + l; ip++) {
        value = *ip;
        jp = ip - 1;
        while((jp &gt;= tab) &amp;&amp; (value &lt; *jp)) {
            *(jp + 1) = *jp;
            --jp;
        }   
        *(jp + 1) = value;
    }   
    return 0;
}

int bubble_sort(int *tab, int l)
{
    int *ip, *jp;
    printf(&quot;bubble sorting!\n&quot;);
    for (jp = tab + l - 2; jp &gt;= tab; jp--) {
        for (ip = tab; ip &lt;= jp; ip++) {
            if (*ip &gt; *(ip + 1)) 
                swap (ip, ip + 1); 
        }   
    }   
    return 0;
}

int select_sort(int *tab, int l)
{
    int *ip, *jp;
    int min;
    printf(&quot;select sorting!\n&quot;);
    for (jp = tab; jp &lt; tab + l - 1; jp++) {
        min = *jp;
        for (ip = jp + 1; ip &lt; tab + l; ip++)
            if (*ip &lt; min)
                swap(ip, &amp;min);
        *jp = min;
    }   
    return 0;
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ve done the C versions a long time ago, and I just revisited this exercise while teaching myself python. Thought I&#8217;d post these here.</p>
<pre class="brush: python;">
def bsort(tab):
    for i in range(len(tab) - 1):
        for j in range(i + 1, len(tab)):
            if tab[j] &lt; tab[i]:
                tab[i], tab[j] = tab[j], tab[i]

def ssort(tab):
    for i in range(len(tab) - 1):
        min = tab[i]
        indexmin = i
        for j in range(i + 1, len(tab)):
            if tab[j] &lt; min:
                min = tab[j]
                indexmin = j
        if indexmin != i:
            tab[i], tab[indexmin] = tab[indexmin], tab[i]

def isort(tab):
    for i in range(1, len(tab)):
        buffer = tab[i]
        j = i - 1
        while j &gt;= 0 and tab[j] &gt; buffer:
            tab[j + 1] = tab[j]
            j -= 1
        tab[j + 1] = buffer
</pre>
<p>and the C versions:</p>
<pre class="brush: cpp;">
int insert_sort(int *tab, int l)
{
    int *ip, *jp;
    int value;
    printf(&quot;insert sorting!\n&quot;);
    for (ip = tab + 1; ip &lt; tab + l; ip++) {
        value = *ip;
        jp = ip - 1;
        while((jp &gt;= tab) &amp;&amp; (value &lt; *jp)) {
            *(jp + 1) = *jp;
            --jp;
        }
        *(jp + 1) = value;
    }
    return 0;
}

int bubble_sort(int *tab, int l)
{
    int *ip, *jp;
    printf(&quot;bubble sorting!\n&quot;);
    for (jp = tab + l - 2; jp &gt;= tab; jp--) {
        for (ip = tab; ip &lt;= jp; ip++) {
            if (*ip &gt; *(ip + 1))
                swap (ip, ip + 1);
        }
    }
    return 0;
}

int select_sort(int *tab, int l)
{
    int *ip, *jp;
    int min;
    printf(&quot;select sorting!\n&quot;);
    for (jp = tab; jp &lt; tab + l - 1; jp++) {
        min = *jp;
        for (ip = jp + 1; ip &lt; tab + l; ip++)
            if (*ip &lt; min)
                swap(ip, &amp;min);
        *jp = min;
    }
    return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dharkael</title>
		<link>http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/#comment-730</link>
		<dc:creator><![CDATA[Dharkael]]></dc:creator>
		<pubDate>Wed, 28 Oct 2009 22:37:02 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1538#comment-730</guid>
		<description><![CDATA[I&#039;m just learning to think in a functional language so this code is probably not as neat or efficient as it could be.
The functions are called with any of Clojure&#039;s  sequence data types, b-sort! and sel-sort! return sorted vectors and in-sort!
returns a sorted Lazy sequence.
By using compare we can also sort sequences containing nil items 
Here goes..
[sourcecode lang=&quot;css&quot;]

(defn in-sort! [data]
  (letfn [(insert ([raw x](insert [] raw x))
		  ([sorted [y &amp; ys :as raw] x]
		     (if (empty? raw) (conj sorted x)
			 (if (neg? (compare x y )) (concat sorted [x,y] ys )
			     (recur (conj sorted y)  ys x )))))]	
    (reduce insert [] data)))

(defn sel-sort! [coll]
  (let [len (count coll)]
    (letfn [(min-index [coll start]
		       (if (=  start len) start
			   (reduce #(if (neg? (compare (nth coll %1) (nth coll %2))) %1 %2) (range start len))))	     
	    (vswap! [a-vec x y] (assoc a-vec x (nth a-vec y) y (nth a-vec x)))]
      (loop [cur 0
	     data (vec coll)
	     min (min-index data cur)]
	(if (&gt;= cur len) data
	    (let [data (vswap! data  min cur)
		  cur (inc cur)]
	      (recur cur data (min-index data cur))))))))

(defn b-sort! [coll]
  (let [cnt (dec (count coll))
	swap (fn [n m data] (assoc data n (nth data m) m (nth data n)))]
    (loop [changed false, n 0,data (vec coll)]
      (cond (&gt;= n cnt)
	    (if changed (recur false 0 data) data)
	    (neg? (compare (nth data n) (nth data (inc n))))
	    (recur changed (inc n) data)
	    :else
	    (recur true (inc n) (swap n (inc n) data))))))

(def data &#039;(6 8 5 9 3 2 1 4 7))

(println &quot;b-sort!:&quot; (b-sort! data))
(println &quot;sel-sort!:&quot; (sel-sort! data))
(println &quot;in-sort!:&quot; (in-sort! data))
;Should print
;b-sort!: [1 2 3 4 5 6 7 8 9]
;sel-sort!: [1 2 3 4 5 6 7 8 9]
;in-sort!: (1 2 3 4 5 6 7 8 9)

[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m just learning to think in a functional language so this code is probably not as neat or efficient as it could be.<br />
The functions are called with any of Clojure&#8217;s  sequence data types, b-sort! and sel-sort! return sorted vectors and in-sort!<br />
returns a sorted Lazy sequence.<br />
By using compare we can also sort sequences containing nil items<br />
Here goes..</p>
<pre class="brush: css;">

(defn in-sort! [data]
  (letfn [(insert ([raw x](insert [] raw x))
		  ([sorted [y &amp; ys :as raw] x]
		     (if (empty? raw) (conj sorted x)
			 (if (neg? (compare x y )) (concat sorted [x,y] ys )
			     (recur (conj sorted y)  ys x )))))]
    (reduce insert [] data)))

(defn sel-sort! [coll]
  (let [len (count coll)]
    (letfn [(min-index [coll start]
		       (if (=  start len) start
			   (reduce #(if (neg? (compare (nth coll %1) (nth coll %2))) %1 %2) (range start len))))
	    (vswap! [a-vec x y] (assoc a-vec x (nth a-vec y) y (nth a-vec x)))]
      (loop [cur 0
	     data (vec coll)
	     min (min-index data cur)]
	(if (&gt;= cur len) data
	    (let [data (vswap! data  min cur)
		  cur (inc cur)]
	      (recur cur data (min-index data cur))))))))

(defn b-sort! [coll]
  (let [cnt (dec (count coll))
	swap (fn [n m data] (assoc data n (nth data m) m (nth data n)))]
    (loop [changed false, n 0,data (vec coll)]
      (cond (&gt;= n cnt)
	    (if changed (recur false 0 data) data)
	    (neg? (compare (nth data n) (nth data (inc n))))
	    (recur changed (inc n) data)
	    :else
	    (recur true (inc n) (swap n (inc n) data))))))

(def data '(6 8 5 9 3 2 1 4 7))

(println &quot;b-sort!:&quot; (b-sort! data))
(println &quot;sel-sort!:&quot; (sel-sort! data))
(println &quot;in-sort!:&quot; (in-sort! data))
;Should print
;b-sort!: [1 2 3 4 5 6 7 8 9]
;sel-sort!: [1 2 3 4 5 6 7 8 9]
;in-sort!: (1 2 3 4 5 6 7 8 9)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michel S.</title>
		<link>http://programmingpraxis.com/2009/10/27/three-quadratic-sorts/#comment-729</link>
		<dc:creator><![CDATA[Michel S.]]></dc:creator>
		<pubDate>Tue, 27 Oct 2009 22:48:40 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1538#comment-729</guid>
		<description><![CDATA[Clojure solution. Bubble and selection sorts are implemented over Java ArrayLists, as they really make no sense to implement in a functional manner. I&#039;ve generalized from the requirement and allow any ordered datatype to be sorted, either using the built-in compare or using a supplied custom comparator.

The insertion sort is done functionally, and can sort over anything Clojure can turn into a sequence (any Clojure collection, ArrayLists, etc.). So as not to clutter the code further, in this case I chose to use numbers only, allowing the use of Clojure&#039;s built-in min function.
 
[sourcecode lang=&quot;css&quot;]
(import &#039;java.util.ArrayList)

(defn arr-swap! [#^ArrayList arr i j]
  (let [t (.get arr i)]
    (doto arr
      (.set i (.get arr j))
      (.set j t))))

(defn bubble-sort!
  ([arr] (bubble-sort! compare arr))
  ([cmp #^ArrayList arr]
     (letfn [(sorter
	      [stop-i]
	      (let [changed (atom false)]
		(doseq [i (range stop-i)]
		  (if (&gt; (cmp (.get arr i) (.get arr (inc i))) 0)
		    (do
		      (arr-swap! arr i (inc i))
		      (reset! changed true))))
		@changed))]
       (doseq [stop-i (range (dec (.size a)) -1 -1)
	       :while (sorter stop-i)])
       arr)))

(defn sel-sort!
  ([arr] (sel-sort! compare arr))
  ([cmp #^ArrayList arr]
     (let [n (.size arr)]
       (letfn [(move-min!
		[start-i]
		(loop [i start-i]
		  (when (&lt; i n)
		    (when (&lt; (cmp (.get arr i) (.get arr start-i)) 0)
		      (arr-swap! arr start-i i))
		    (recur (inc i)))))]
	 (doseq [start-i (range (dec n))]
	   (move-min! start-i))
	 arr))))

(defn ins-sort
  [xs]
  (letfn [(remove-first
	   [x xs]
	   (if (= x (first xs)) (next xs)
	       (cons (first xs) (remove-first x (next xs)))))
	  (sorter
	   [xs]
	   (if (or (empty? xs) (empty? (next xs))) xs
	       (let [x (apply min xs)
		     xs1 (remove-first x xs)]
		 (cons x (sorter xs1)))))]
       (sorter (seq xs))))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Clojure solution. Bubble and selection sorts are implemented over Java ArrayLists, as they really make no sense to implement in a functional manner. I&#8217;ve generalized from the requirement and allow any ordered datatype to be sorted, either using the built-in compare or using a supplied custom comparator.</p>
<p>The insertion sort is done functionally, and can sort over anything Clojure can turn into a sequence (any Clojure collection, ArrayLists, etc.). So as not to clutter the code further, in this case I chose to use numbers only, allowing the use of Clojure&#8217;s built-in min function.</p>
<pre class="brush: css;">
(import 'java.util.ArrayList)

(defn arr-swap! [#^ArrayList arr i j]
  (let [t (.get arr i)]
    (doto arr
      (.set i (.get arr j))
      (.set j t))))

(defn bubble-sort!
  ([arr] (bubble-sort! compare arr))
  ([cmp #^ArrayList arr]
     (letfn [(sorter
	      [stop-i]
	      (let [changed (atom false)]
		(doseq [i (range stop-i)]
		  (if (&gt; (cmp (.get arr i) (.get arr (inc i))) 0)
		    (do
		      (arr-swap! arr i (inc i))
		      (reset! changed true))))
		@changed))]
       (doseq [stop-i (range (dec (.size a)) -1 -1)
	       :while (sorter stop-i)])
       arr)))

(defn sel-sort!
  ([arr] (sel-sort! compare arr))
  ([cmp #^ArrayList arr]
     (let [n (.size arr)]
       (letfn [(move-min!
		[start-i]
		(loop [i start-i]
		  (when (&lt; i n)
		    (when (&lt; (cmp (.get arr i) (.get arr start-i)) 0)
		      (arr-swap! arr start-i i))
		    (recur (inc i)))))]
	 (doseq [start-i (range (dec n))]
	   (move-min! start-i))
	 arr))))

(defn ins-sort
  [xs]
  (letfn [(remove-first
	   [x xs]
	   (if (= x (first xs)) (next xs)
	       (cons (first xs) (remove-first x (next xs)))))
	  (sorter
	   [xs]
	   (if (or (empty? xs) (empty? (next xs))) xs
	       (let [x (apply min xs)
		     xs1 (remove-first x xs)]
		 (cons x (sorter xs1)))))]
       (sorter (seq xs))))
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

