Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream.h>


(main) void
{
        int i, j, first, temp;
      int numLength = num.length( );
        for (i= numLength - 1; i > 0; i--)
      {
              first = 0;
              for (j=1; j<=i; j++)
             {
                      if (num[j] < num[first])
                      first = j;
             }
            temp = num[first];
         num[first] = num[i];
         num[i] = temp;
     }
     return;
}


[edit]Code block added, HTML encoded - OriginalGriff[/edit]
Posted
Updated 12-May-20 19:27pm
v4
Comments
Richard C Bishop 8-Jan-13 14:23pm    
This is not a question. Elaborate on your issue and explain what you are trying to accomplish. Then, form a specific question on what you need help with.
OriginalGriff 8-Jan-13 14:23pm    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
You seem to have forgotten to tell us what your problem is...
Use the "Improve question" widget to edit your question and provide better information.
wazir ahmad 8-Jan-13 14:35pm    
i need slection sort example plz share who its work

My code to sort an array in descending order by selection sort:-
.
.
.
#include <bits/stdc++.h> 
using namespace std; 
  
void swap(int *xp, int *yp)  
{  
    int temp = *xp;  
    *xp = *yp;  
    *yp = temp;  
}  
  
void selectionSort(int arr[], int n)  
{  
    int i, j, max_idx;  
   
    for (i = 0; i < n-1; i++)  
    {    
        max_idx = i;  
        for (j = i+1; j < n; j++)  
        if (arr[j] > arr[max_idx])  
            max_idx = j;    
        swap(&arr[max_idx], &arr[i]);  
    }  
}  
void printArray(int arr[], int size)  
{  
    int i;  
    for (i=0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
}  
  
// Driver program to test above functions  
int main()  
{  
    int arr[] = {64, 25, 12, 22, 11};  
    int n = sizeof(arr)/sizeof(arr[0]);  
    selectionSort(arr, n);  
    cout << "Sorted array: \n";  
    printArray(arr, n);  
    return 0;  
}  
 
Share this answer
 
v4
Comments
CHill60 13-May-20 4:54am    
See my comment on your other solution to an 8 year old question. Uncommented code dumps are not helpful
Your code won't compile, even on the ancient compiler you are using (why don't you use a more modern C++ compiler?).

You probably mean:
C++
#include <iostream.h>

// the selection sort function
void sort(int num[], int numLength);

// usage sample
int main()
{
	int v[] =  {0,7,-5, 10000, 45};
	const int SIZE = sizeof(v)/sizeof(v[0]); 
	sort(v, SIZE);
	for (int i=0; i<SIZE; i++)
		cout << v[i] << " ";
	cout << endl;
}
// sort implementation
void sort(int num[], int numLength)
{
  int i, j, first, temp;
  if ( numLength < 2) return;

  for (i = numLength - 1; i > 1; i--)
  {
    first = 0;
    for (j=1; j<=i; j++)
    {
       if (num[j] < num[first])
	 first = j;
    }
    temp = num[first];
    num[first] = num[i];
    num[i] = temp;
  }
 
Share this answer
 
Comments
nv3 8-Jan-13 16:15pm    
Hey, you are way too good to these guys who are trying to get their homework done :-)
CPallini 9-Jan-13 3:01am    
:-)
Looks like a homework to me. but google search would list your result.

Check here[^]
 
Share this answer
 
This is not a question at all. The one thing I will say is, iostream.h is not C++. Just include iostream without the .h. Apart from that, this is a code dump. If you have a question, edit this post, and ask it. If not, it will be deleted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900