Question:
Implementing sorting algorithms C++?
Cutout
2012-04-25 00:09:38 UTC
I need to implement all these algorithms in C++ and I'm a beginner. Could you please help me out in any way possible?
1) counting sort,
2) bubble sort,
3) selection sort,
4) insertion sort,
6) merge sort,
5) quicksort,
7) LSD radix sort

Thank you very much.
Three answers:
?
2012-04-25 00:39:29 UTC
Sample code for each of the algorithms except #1 and #7 can be found here: http://programmingnotes.freeweq.com/?p=918
Casper
2012-04-25 00:37:07 UTC
hey... i cant paste whole code here. there is some limitation here. any way i will place some of it. all those are in java. don't worry. there are not much to modify.. these function may look lengthy but simplest for you . easy to understand. ok. no complex coding i have done. thinking you are a beginner .. all you have to change is "input.length " to length of the array, pass the length of array to the function. so change the function to public int[] *******Sort(int[] input, SortMode sortMode, int length)

// I use this enum to set the mode of sorting. my sort functions use this

enum SortMode

{

Ascending ,Descending

};

// this method is a must for my code.

private void swap(int[] input, int i , int j)

{

int temp ;

temp = input[i];

input[i]= input[j];

input[j]= temp;

}



// Insertion sort

public int[] InsertionSort(int[] input, SortMode sortMode)

{

if( sortMode == SortMode.Ascending)

{

for( int i = 1 ; i < input.length ; i++ )

{

for( int j = 0 ; j < i ; j++ )

{

if( input [j] > input [i] )

{

swap(input , i , j);

}

}

}

}

else

{

for( int i = 1 ; i < input.length ; i++ )

{

for( int j = 0 ; j < i ; j++ )

{

if( input [j] < input [i] )

{

swap(input , i , j);

}

}

}

}

return input;

}

// Selection Sort



public int[] SelectionSort(int[] input, SortMode sortMode)

{

if (sortMode == SortMode.Ascending)

{

for (int i = 0 ; i < input.length ; i++ )

{

for(int j = i+1 ; j < input.length ; j++ )

{

if( input [i] > input[j])

{

swap(input, i, j);

}

}

}

}

else

{

for (int i = 0 ; i < input.length ; i++ )

{

for(int j = i+1 ; j < input.length ; j++ )

{

if( input [i] < input[j])

{

swap(input, i, j);

}

}

}

}

return input;

}



for the rest .. if you can provide email id .. i can mail you.. ( don't worry .i am not a spammer. still you think i am, create a temporary email id and give me )
2012-04-25 00:18:34 UTC
insertion sort,


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...