Swapping in Array

I am trying to write a program to sort an array of 10 numerical values from smallest to largest
I'm lost at the swapping part. How do I write that part so I am able to swap the values so they are listed from smallest to largest?

Here's what I have so far:

1
2
3
4
5
6
7
#include "stdafx.h"
#include <iostream>

using namespace std;


Last edited on
The lines 8-10 have quite descriptive instructions, but lets look at two details first:
1. What does happen on line 32?
2. What does happen on line 39?
In 32 I'm setting my array size, and 39 is when I input the 10 numbers I want to sort.
Is this what you were asking?
In 32 you introduce a new variable and set its value.
In 39 you access cell i of your array. (Copy value to.)
What you are trying to implement is a bubble sort algorithm.
Swaping values, the easy way is to use a third temporay variable like so:
int temp = i;
i = j;
j = temp;
With this 3 lines the values ov i and j are swaped.
Also you could read about bubblesort and make the algorithm a little bit better only using 1 function for the sorting.

Here, have a look at this link:
http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
Topic archived. No new replies allowed.