Sorting with a stopwatch.

ddanbe 1 Tallied Votes 527 Views Share

OK Quicksort IS fast. But what if you just wanna sort 30 items? Then Quicksort becomes a bit of overkill. So what most people then use is the one and only popular "bubblesort", also called "standard exchange sort". Let me present you here with my implementation of another sort(among many others) the "straight insertion sort". It's the same technique cardplayers use to place their cards in order. With the Stopwatch class you can even proof that the straight insertion sort of 10 integers is 3 to 4 times faster then bubblesort! So dump bubblesort! Straight insertion sort is as easy or even easier to implement.
See for yourself in these code snippets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;       //for stopwatch

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        { 
            Stopwatch SW = new Stopwatch();
            SW.Start();
            int[] Myar = { 7, 6, 2, 10, 1, 7, 3, 8, 5, 9 };   
            BubbleSort(Myar);
            for(int i = 0; i < Myar.Length; i++)
                Console.WriteLine(Myar[i]);
            SW.Stop();
            Console.WriteLine("Bubble sort = {0} elapsed ticks.", SW.ElapsedTicks);
            SW.Reset();
            SW.Start();
            int[] Myar2 = { 7, 6, 2, 10, 1, 7, 3, 8, 5, 9 };    //same as Myar
            StraightInsertionSort(Myar2);
            for (int i = 0; i < Myar2.Length; i++)
                Console.WriteLine(Myar2[i]);
            SW.Stop();
            Console.WriteLine("Straight insertion sort = {0} elapsed ticks.", SW.ElapsedTicks);
            Console.ReadKey();
        }

        //sort the items of an array using straight insertion sort
        //
        static void StraightInsertionSort(int[] listtosort)
        {
            int element;
            int ic;     //index of element to be compared, index starts at 0

            for (int loopvar = 1; loopvar < listtosort.Length; loopvar++)  //loop trough each element in turn
            {
                element = listtosort[loopvar];
                ic = loopvar-1;
                while (ic >= 0 && listtosort[ic] > element ) 
                {
                    listtosort[ic + 1] = listtosort[ic];    //move all elements
                    ic--;
                }
                listtosort[ic + 1] = element;    //Insert element
            }           
        } 
       
        // sort the items of an array using bubble sort 
        //
        public static void BubbleSort( int[] ar )      
        {         
            for ( int pass = 1; pass < ar.Length; pass++ )            
                for ( int i = 0; i < ar.Length - 1; i++ )               
                    if ( ar[ i ] > ar[ i + 1 ] )                 
                        Swap( ar, i );      
        }       
        
        // swap two items of an array
        //
        public static void Swap( int[] ar, int first )      
        {         
            int hold;          
            hold = ar[ first ];         
            ar[ first ] = ar[ first + 1 ];         
            ar[ first + 1 ] = hold;      
        }
    }
}
ddanbe 2,724 Professional Procrastinator Featured Poster

StopWatch is not completly correct here : read http://www.codeproject.com/KB/dotnet/ExecutionStopwatch.aspx
the sorting algorithm are!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.