# code for sorting arrays?

I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
Thanks!

• : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: Thanks!
:

The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):
[code]
#include

#define NULEM(arr) (sizeof(arr)/sizeof(arr[0]))

/*RETURNS THE NUMBER OF ELEMENTS IN AN ARRAY*/

int[] BubbleSort(int[]);

void main()
{
int arr[] = {3,2,1}
int nulm;
for(nulm=0;nulm < NULEM(arr);nulm++)
arr = BubbleSort(arr);
}

int[] BubbleSort(int arr[])
{
int nulm, temp;
for(nulm=0;nulm < NULEM(arr);nulm++)
{
if(arr[nulm]>arr[nulm+1])
{
temp = arr[nulm];
arr[nulm] = arr[nulm + 1];
arr[nulm + 1] = temp;
}
}

return(arr);
}

[/code]

• I'll try it out with my array, thanks!

• : : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : Thanks!
: :
:
: The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):

It'd make much more sense to put both loops inside the function. Having one loop outside of it means everytime you call the bubblesort function you have to duplicate code that really shouldnt be duplicated.
• : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: Thanks!
:

You should find something useful at some of these:
http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htm
http://cs.smith.edu/~thiebaut/java/sort/
http://atschool.eduweb.co.uk/mbaker/sorts.html
http://math.baruch.cuny.edu/~bshaw/index-8.html
http://linux.wku.edu/~lamonml/algor/sort/
http://www.dcc.uchile.cl/~rbaeza/handbook/sort_a.html
http://www.cs.hope.edu/~alganim/ccaa/harrison.html

----------------
Walt

• Wow, these links are great, thanks Walt!!!!

: : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : Thanks!
: :
:
: You should find something useful at some of these:
: http://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htm
: http://cs.smith.edu/~thiebaut/java/sort/
: http://atschool.eduweb.co.uk/mbaker/sorts.html
: http://math.baruch.cuny.edu/~bshaw/index-8.html
: http://linux.wku.edu/~lamonml/algor/sort/
: http://www.dcc.uchile.cl/~rbaeza/handbook/sort_a.html
: http://www.cs.hope.edu/~alganim/ccaa/harrison.html
:
:
:
: ----------------
: Walt
:
:
:

• : : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : Thanks!
: :
:
: The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):
: [code]
: #include
:
: #define NULEM(arr) (sizeof(arr)/sizeof(arr[0]))
:
: /*RETURNS THE NUMBER OF ELEMENTS IN AN ARRAY*/
:
: int[] BubbleSort(int[]);
:
: void main()
: {
: int arr[] = {3,2,1}
: int nulm;
: for(nulm=0;nulm < NULEM(arr);nulm++)
: arr = BubbleSort(arr);
: }
:
: int[] BubbleSort(int arr[])
: {
: int nulm, temp;
: for(nulm=0;nulm < NULEM(arr);nulm++)
: {
: if(arr[nulm]>arr[nulm+1])
: {
: temp = arr[nulm];
: arr[nulm] = arr[nulm + 1];
: arr[nulm + 1] = temp;
: }
: }
:
: return(arr);
: }
:
: [/code]
:
: I will be happy to answer any question about this code - WEREWOLF
:

your examble code is good for someone to understand it but
it cannot be compiled :P you cant use macros in functions
(like in the examble)
• : : : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : : Thanks!
: : :
: :
: : The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):
:
:
: It'd make much more sense to put both loops inside the function. Having one loop outside of it means everytime you call the bubblesort function you have to duplicate code that really shouldnt be duplicated.
:
how the code is duplicated?, as only one space is allocated for function. so if you call every time no problem only that space is used. no duplication. And using function is a good tech. I recommend it. though here the code is small but the habit will help.
The only space extra required is to store the return address on stack and necessary variables.
Both the loops can be used in the function but the above code is also efficient.

Gourav Tilve
email: gdtilve@hotmail.com

• : : : : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : : : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : : : Thanks!
: : : :
: : :
: : : The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):
: :
: :
: : It'd make much more sense to put both loops inside the function. Having one loop outside of it means everytime you call the bubblesort function you have to duplicate code that really shouldnt be duplicated.
: :
: how the code is duplicated?, as only one space is allocated for function. so if you call every time no problem only that space is used. no duplication. And using function is a good tech. I recommend it. though here the code is small but the habit will help.
: The only space extra required is to store the return address on stack and necessary variables.
: Both the loops can be used in the function but the above code is also efficient.

In that example code isn't duplicated. But lets say I had 10 arrays I want sorted at different times. The number of required FOR loops would be 11 (one inside the function, and one for each time I want an array sorted). If the function was written better, it would have had both loops internally. And in that case, the required code would only contain 2 loops (both inside the function).
• : : I need to sort an array using quicksort, selection sort, and bubblesort or insertion sort.
: : Anyone know of some good sites to find the code I need? I've looked at some tutorials, but I'm not sure how to implement what I've seen to my arrays in particular.
: : Thanks!
: :
:
: The best one I understanded was bubble sort (C language code)(the sort is from the lowest number to the highest):
: [code]
: #include
:
: #define NULEM(arr) (sizeof(arr)/sizeof(arr[0]))
: /*RETURNS THE NUMBER OF ELEMENTS IN AN ARRAY*/
[/code]
That's a handy macro, and I use one of these myself, but NULEM sounds too much like NULL. Usually, the abbreviation is simply NELEM or even NELEMS.
With NULEM, I think about looking for the sentinel value in an array of pointers or something.
[code]
: int[] BubbleSort(int[]);
[/code]
This is not going to work, a function cannot return an array. I can think of two options here to fix it. First, you can change the return type to simply pointer to int, which is likely to be what you meant anyway. Second, if you aren't returning a sorted copy of the array, then don't bother returning anything.
[code]
: void main()
[/code]
Oops! That should be "int main(void)" for C. No void necessary in the argument list for C++, but both require that main return int in a portable program.
[code]
: {
: int arr[] = {3,2,1}
: int nulm;
: for(nulm=0;nulm < NULEM(arr);nulm++)
: arr = BubbleSort(arr);
: }
[/code]
'arr' is declared as an array, so you cannot assign to it. Array names are not variables.
[code]
: int[] BubbleSort(int arr[])
: {
: int nulm, temp;
: for(nulm=0;nulm < NULEM(arr);nulm++)
[/code]
I think someone already pointed out this one elsethread. You can't use your macro here on the parameter 'arr'. 'arr' in this function is merely a pointer. This is what you must learn: arrays and pointers are not the same thing. They are equivalent as arguments to a function, and the declarations:
[code=ffffff]
int *arr
int arr[]
[/code]
are equivalent only as [b]parameters[/b] to a function. This means that you cannot use this form of declaration as the return value of a function, only as the parameters to it.

Yes, pointers and arrays are equivalent, but that does not mean they are identical and interchangeable.

A worthwhile fix is to change your function to accept two parameters, with the second one being the number of elements in the array.

[code]
: {
: if(arr[nulm]>arr[nulm+1])
: {
: temp = arr[nulm];
: arr[nulm] = arr[nulm + 1];
: arr[nulm + 1] = temp;
: }
: [/code]

If you implement the second parameter and keep this implementation, make sure you only loop to number_of_elements-1, otherwise you will access beyond the bounds of your array.

HTH,
Will