Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,220 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,238 people online right now. Registration is fast and FREE... Join Now!





BubbleSort an array in C++

This is a snippet that will perform a BubbleSort on a string (char) array

Submitted By: PsychoCoder
Actions:
Rating:
Views: 218

Language: C++

Last Modified: August 7, 2008

Snippet


  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. void PerformBubbleSort(const char *strArray[] , int arraySize)
  8. {
  9.         int compare;
  10.         //loop throgh the size of the array
  11.         for (int i = 0; i < arraySize - 1 ;++i)
  12.         {
  13.                 //loop through each word in the array
  14.                 for (int j = 0; j < arraySize - 1 - i; ++j)
  15.                 {
  16.                         //compare the first character to see if they match
  17.                         compare = strcmp (strArray[j], strArray[j+1]);
  18.  
  19.                         //check the value of the comparison
  20.                         if (compare > 0)
  21.                         {
  22.                                 //they do not match so call swap to arrange the current list
  23.                                 swap (strArray[j] , strArray[j+1]);
  24.                         }
  25.                 }
  26.         }
  27. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month