A Bubble sort : Sort « Development « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Development » Sort 
5.22.1.A Bubble sort
#include <iostream> 
#include <cstdlib> 
using namespace std; 
 
int main() 

  int nums[10]
  int a, b, t; 
  int size; 
 
  size = 10// number of elements to sort 
 

  for(t=0; t<size; t++
      nums[t= rand()
 
  cout << "Original array is:\n   "
  for(t=0; t<size; t++
      cout << nums[t<< ' '
  cout << '\n'
 
  // This is the bubble sort. 
  for(a=1; a<size; a++
    for(b=size-1; b>=a; b--) { 
      if(nums[b-1> nums[b]) { // if out of order 
        // exchange elements  
        t = nums[b-1]
        nums[b-1= nums[b]
        nums[b= t; 
      
    
 
  cout << "\nSorted array is:\n   "
  for(t=0; t<size; t++
      cout << nums[t<< ' '
 
  return 0
}
Original array is:
   41 18467 6334 26500 19169 15724 11478 29358 26962 24464

Sorted array is:
   41 6334 11478 15724 18467 19169 24464 26500 26962 29358
5.22.Sort
5.22.1.A Bubble sort
5.22.2.A recursive version of Quicksort for sorting characters
5.22.3.Quick Sort
5.22.4.how to declare your own function and function pointer to be used with qsort( )
5.22.5.Sort Tracer
5.22.6.Selection Sort
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.