Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Friday, November 9, 2018

This program uses the bubble sort algorithm to sort an string array in alphabet order

// This program have been developed by changing program Pr8-4
// Array of integer numbers have been changed to array of strings.
#include "stdafx.h"
#include <string>       // Add this !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Pr8-4 for strings
// This program uses the bubble sort algorithm to sort an
// string array in alphabet order.
#include <iostream>
using namespace std;

// Function prototypes
void sortArray(string [], int);
void showArray(string [], int);

int main()
{
// Array of unsorted strings.
string values[6] = {"Hussein","Zachary" ,"Diarra", "Chris", "Delong", "Kenneth"};

// Display the strings.
cout << "The unsorted values are:\n";
showArray(values, 6);

// Sort the strings.
sortArray(values, 6);

// Display them again.
cout << "The sorted values are:\n";
showArray(values, 6);
return 0;
}

//***********************************************************
// Definition of function sortArray *
// This function performs an ascending order bubble sort on *
// array. size is the number of elements in the array. *
//***********************************************************

void sortArray(string array[], int size)
{
bool swap;
string temp;

do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}

//*************************************************************
// Definition of function showArray. *
// This function displays the contents of array. size is the *
// number of elements. *
//*************************************************************

void showArray( string array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << "\n";
cout << endl;
}

Basic Comparison of C++ and HTML/JavaScript

 Here’s a basic comparison of C++ and HTML/JavaScript for common programming concepts and  simple code examples for each. This will help ...