Question

How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't.

Here's my code.

#include <iostream>

using namespace std;

void selectionSort(int[], int, int& );
void showSelection(int[], int);
void sortArray(int[], int, int&);
void showArray(const int[], int);

int main()
{
   int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };

   cout << "The values are \n";
   showArray(values, 25);

   int counter1 = 0, counter2 = 0;

   sortArray(values, 25, counter1);

   cout << "the sorted values are sorted with the bubble sort :\n";
   showArray(values, 25);
   cout << "Number of exhanges with bubble sort: " << counter1 << endl;

   selectionSort(values, 25, counter2);

   cout << "The other sorted values are sorted with selection sort : \n";
   showArray(values, 25);
   cout << " Number of exhanges by selection sort: " << counter2 << endl;

   return 0;

}

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

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

void selectionSort(int array[], int size, int &e)
{
   int startScan, minindex, minValue;

   for (startScan = 0; startScan < (size - 1); startScan++)
   {
       minindex = startScan;
       minValue = array[startScan];
       for (int index = startScan + 1; index < size; index++)
       {
           if (array[index] < minValue)
           {
               minValue = array[index];
               minindex = index;
           }
       }
       array[minindex] = array[startScan];
       array[startScan] = minValue;
       e++;
   }
}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Its working fine and giving output of how many times the values exchange in linear sort or bubble sort..

Here is code of yours:

#include <iostream>

using namespace std;

void selectionSort(int[], int, int &);

void showSelection(int[], int);

void sortArray(int[], int, int &);

void showArray(const int[], int);

int main()

{

int values[25] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24};

cout << "The values are \n";

showArray(values, 25);

int counter1 = 0, counter2 = 0;

sortArray(values, 25, counter1);

cout << "the sorted values are sorted with the bubble sort :\n";

showArray(values, 25);

cout << "Number of exhanges with bubble sort: " << counter1 << endl;

selectionSort(values, 25, counter2);

cout << "The other sorted values are sorted with selection sort : \n";

showArray(values, 25);

cout << " Number of exhanges by selection sort: " << counter2 << endl;

return 0;

}

void sortArray(int array[], int size, int &e)

{

bool swap;

int temp;

do

{

swap = false;

for (int count = 0; count < (size - 1); count++)

{

if (array[count] < array[count + 1])

{

e++;

temp = array[count];

array[count] = array[count + 1];

array[count + 1] = temp;

swap = true;

}

}

} while (swap);

}

void selectionSort(int array[], int size, int &e)

{

int startScan, minindex, minValue;

for (startScan = 0; startScan < (size - 1); startScan++)

{

minindex = startScan;

minValue = array[startScan];

for (int index = startScan + 1; index < size; index++)

{

if (array[index] < minValue)

{

minValue = array[index];

minindex = index;

}

}

array[minindex] = array[startScan];

array[startScan] = minValue;

e++;

}

}

void showArray(const int array[], int size)

{

for (int count = 0; count < size; count++)

cout << array[count] << " " << endl;

}

Output:

The values are 1 7 9 11 15 17 19 21 25 2 16 12 14 28 22 24 the sorted values are sorted with the bubble sort : 24 23 22 19 18

Add a comment
Know the answer?
Add Answer to:
How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code...

    Selection sort is often the first sorting algorithm covered in introductory computer science courses. Java code that uses selection sort to place the elements of an integer array into non-decreasing order is shown here: public void swapNumbers(int i, int j) { ​int temp = numbers[i];​ /* put numbers[i] somewhere to keep it safe */ ​numbers[i] = numbers[j]; /* put numbers[j] at index i */ ​numbers[j] = temp;​ /* put numbers[i] at index j */ } public void selectionSort(int[] numbers) {...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Language C++ (Please include a short description & Screenshot of output) Implement a Priority...

    Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT