I need to write a program that sorts some data into ascending order. I was looking in my programming book and found a sample program that does that such thing. I am a little confused on some of the aspects of the code they used, and they didn't comment on it very well so i am not sure what's going on. I was wondering if someone could give a few insights into why some things are the way they are so that I can write my own program. Book code is as follows:

void sort(int x[], int npts)
{
	int k, j, m ;
	double hold ;
	
	for (k=0; k <=npts-2 ; k++)
	{
		m = k ;
		for (j=k+1; j <= npts -1; j++)
			if (x[j] < x[m])
				m = j;
			hold = x[m];
			x[m] = x[k];
			x[k] = hold;
	}
	return ;
}

So here are the things I don't have a clue on.

-Why does the first for loop run up to k <=npts-2?
-What is the purpose of the line m = k?
-Why does the second loop start at j = k+1?
-What is the purpose opf the line m = j inside the second loop?
-Why do values get shuffled about in the hold thing?

So basically, I have no idea what is going on here. I figure knowing why this works will help me write the program I have to write...but, I doubt it. Thanks y'all.

Recommended Answers

All 6 Replies

The algorithm in use is selection sort[wikipedia.org]. It's one of the simplest sorting techniques.

It's algorithm is:
1. Find the smallest element in an unsorted array.
2. Swap it with the value at the first position for the current iteration.
3. Repeat this procedure n-1 times. (n being the no of elements in the array)

If you have trouble understanding the algorithm, check out the animation in the wikipedia link posted above.

To answer your questions:
-The outer for loop is there to accomplish the third step in the algorithm.
-At the start of each iteration, you assign the topmost element for the current iteration to m(this variable holds the minimum value in the array for the current iteration)
-The inner loop scans the rest of the array and tries to see if there are elements with smaller values than what has already been assigned to 'm'.
-If there are such elements, we place it in 'm' since it holds the value of the smallest element.
-After finding the least element, we place it at the top of the unsorted part of the array(that is, the position pointed to by the outer loop)

Small correction: The variable 'm' doesn't hold the value of the smallest element, but it holds the index of the smallest element in the array.

I have no idea why his algorithm had taken variable 'hold' as double instead of int?

I have no idea why his algorithm had taken variable 'hold' as double instead of int?

Instead of float,many times double is used and here hold is the variable of type double.

Instead of float,many times double is used and here hold is the variable of type double.

you are doing somthing like this in for-loop:

hold = x;

where x[] is an array of int.

you are doing somthing like this in for-loop:

hold = x;

where x[] is an array of int.

I agree to your point .We can also do the same thing using

int hold;
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.