google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: C++ program to implement Selection Sort

Friday, December 27, 2019

C++ program to implement Selection Sort

C++ program to implement Selection Sort:

#include

using namespace std;

void swap(int &a, int &b)
{     
   int temp;
   temp = a;
   a = b;
   b = temp;
}

void display(int *array, int size)
{
   for(int i = 0; i      cout << array[i] << " ";
   cout << endl;
}

void selectionSort(int *array, int size)
{
   int i, j, k;
   for(i = 0; i   {
      k = i; 
      for(j = i+1; j         if(array[j] < array[k])
            k = j;
            swap(array[i], array[k]);
   }
}

int main()
{
   int n;
   cout << "Enter the number of elements: ";
   cin >> n;
   int arr[n];         
   cout << "Enter elements:" << endl;
   for(int i = 0; i {
      cin >> arr[i];
   }
   cout << "Array before Sorting: ";
   display(arr, n);
   selectionSort(arr, n);
   cout << "Array after Sorting: ";
   display(arr, n);
}


Output

Enter the number of elements: 4
Enter elements:
5 9 7 23
Array before Sorting: 5 9 7 23
Array after Sorting: 5 7 9 23 

No comments:

Post a Comment

रामायण

रामायण दशरथ की तीन पत्नियाँ – कौशल्या, सुमित्रा , कैकेयी दशरथ के चार पुत्र – राम,लक्ष्मण,भरत,शत्रुघ्न दशरथ: राम के पिता और कौशल के राजा कौशल...