Total members 11743 |It is currently Sat Sep 22, 2018 5:31 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





C++ selection sorting implementation.
cpp code
#include <iostream>

using namespace std;

/*
* Program to read a list of integers from standard input and print them in
* sorted order. This uses a simple selection sort.
*
* Author: Tom Bennet
*/

/*
* Swap function for sorting.
*/
void swap(int *a, int *b)
{
int tmp; /* Exchange temp value. */

tmp = *a;
*a = *b;
*b = tmp;
}

/*
* Sort the array. It receives a pointer to the start of the data and the
* number of items.
*/
void sort(int *data, int size)
{
int minloc; /* Loc of minimum. */

for(minloc = 0; minloc < size - 1; ++minloc) {
int scan;
for(scan = minloc + 1; scan < size; ++scan)
if(data[minloc] > data[scan])
swap(&data[minloc], &data[scan]);
}
}

/*
* Main program. Reads the integers into an array, sorts them, and
* prints them out.
*/
const int MAX_NUM_INTS = 100;
int main()
{
int ints[MAX_NUM_INTS]; // Where the numbers go.

// Read them in.
int i;
for(i = 0; i < MAX_NUM_INTS && cin >> ints[i]; ++i);
int numints = i;

// Sort them.
sort(ints, numints);

// Print them.
cout << "==================" << endl;
for(int i = 0; i < numints; ++i)
cout << ints[i] << endl;
cout << "==================" << endl;
}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : C++ Selection Sort
 selection sort     -  
 Help with selection sort for strings?     -  
 fill selection options from list     -  
 selection of checkbox in buttongroup in netbeans     -  
 Good looking selection box with image per option     -  
 validate age entered as selection box in javascript     -  
 calculate data from multiple selection     -  
 how to load the form elements depending on selection option     -  
 redirect page based on user selection radio button     -  
 C++ Bubble Sort     -  



Topic Tags

C++ Sorting






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com