Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- // function for displaying unsorted numbers
- // printing the numbers from the array from numbers,ARRAY_SIZE, and count
- void DisplayingUnsortedNumbers (int numbers[], int ARRAY_SIZE, int count)
- {
- // Displaying the unsorted numbers
- cout << " The numbers are: " ;
- for (count = 0; count < ARRAY_SIZE; count ++)
- cout << numbers [count] << " ";
- cout << endl;
- }
- // function for bubble sort
- // Sort numbers of the ARRAY_SIZE using Bubble Sort.
- void BubbleSort (int numbers[], int ARRAY_SIZE)
- {
- bool swapped = true; // boolean function
- int j = 0;
- int tmp; // varible for temporary swap
- while (swapped) // while loop for the bubble sort
- {
- swapped = false;
- j++;
- for (int i = 0; i < ARRAY_SIZE - j; i++)
- {
- if (numbers[i] > numbers[i + 1])
- {
- tmp = numbers[i];
- numbers[i] = numbers[i + 1];
- numbers[i + 1] = tmp;
- swapped = true;
- }
- }
- }
- } // bubble sort function ends here
- //**************
- //* Main function
- //* opens our list and then stores this list into the array
- //* also has our functions for printing the array that is not sorted
- //* function for the bubble sort algorithm and prints the sorted numbers by returning
- //************
- int main ()
- {
- int count = 0; // loop counter varible
- string fileName="file.txt";
- ifstream inputFile; // input file stream object
- //opening the file
- inputFile.open(fileName); // Using ARRAY_SIZE and inputfile so that the text file works with any ammount of numbers
- int ARRAY_SIZE;
- inputFile >> ARRAY_SIZE;
- int numbers[ARRAY_SIZE];
- //reading the numbers from the file then putting it into the array
- while (count < ARRAY_SIZE && inputFile >> numbers [count])
- count ++;
- // closing file
- inputFile.close();
- //function for displaying unsorted numbers
- DisplayingUnsortedNumbers(numbers, ARRAY_SIZE, count);
- // function for sorted numbers
- BubbleSort(numbers, ARRAY_SIZE);
- cout << " The sorted numbers are: " ;
- for (auto num: numbers)
- {
- cout << num << ' ' ;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement