Advertisement
Dani_info

Metode de sortare cpp

Apr 9th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. // Recapitulare sortari.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. void BubbleSort(int[], int);
  10. void SelectionSort(int[], int);
  11. void InsertionSort(int[], int);
  12.  
  13. int main()
  14. {  
  15.     //citire vector;
  16.     int n;
  17.     cout << "Cate elemente are vectorul ?"; cin >> n;
  18.     int v[10];
  19.     cout << "Introdu elementele vectorului:" << endl;
  20.     for (int i = 0; i < n; i++)
  21.         cin >> v[i];
  22.     cout << "Vectorul sortat este:" << endl;
  23.     //BubbleSort(v, n
  24.     //SelectionSort(v, n);
  25.     InsertionSort(v, n);
  26.     system("pause");
  27.     return 0;
  28. }
  29.  
  30. void BubbleSort(int v[], int n) {
  31.     int i = 0;
  32.     int ok;
  33.     do
  34.     {
  35.         ok = 1;
  36.         for (int j = 0; j < n - i - 1; j++)
  37.         {
  38.             if (v[j+1] < v[j])
  39.             {
  40.                 swap(v[j+1], v[j]);
  41.                 ok = 0;
  42.             }
  43.         }
  44.         i++;
  45.     } while (ok==0 && i<n-1);
  46.         for (int i = 0; i < n; i++)
  47.             cout << v[i] << " ";
  48. }
  49.  
  50. void SelectionSort(int v[], int n){
  51.     for (int m = n - 1; m >= 0; m--)
  52.     {
  53.         int nmax = v[0];
  54.         int ind = 0;
  55.         for (int i = 0; i <= m; i++)
  56.         {
  57.             if (v[i] > nmax)
  58.             {
  59.                 nmax = v[i];
  60.                 ind = i;
  61.             }
  62.         }
  63.         swap(v[m],v[ind]);
  64.     }
  65.     for (int i = 0; i < n; ++i)
  66.         cout << v[i] << " ";
  67. }
  68.  
  69. void InsertionSort(int v[], int n) {
  70.     for (int i = 1; i < n; i++)
  71.     {
  72.         int a = v[i];
  73.         int ind = i - 1;
  74.         while (v[ind] > a && ind >= 0)
  75.         {
  76.             v[ind + 1] = v[ind];
  77.             ind--;
  78.         }
  79.         v[ind + 1] = a;
  80.     }
  81.     for (int i = 0; i < n; ++i)
  82.         cout << v[i] << " ";
  83. }
  84. void CountingSort(int v[], int n)
  85. {
  86.     int a[10];
  87.     for (int i = 0; i < n; i++)
  88.         a[i] = 0;
  89.     for (int i = 0; i < n-1; i++)
  90.     {
  91.         for (int j = i + 1; j < n; j++)
  92.         {
  93.             if (v[i] > v[j])
  94.                 a[i]++;
  95.             else
  96.                 a[j]++;
  97.         }
  98.     }
  99.     for (int i = 0; i < n; i++)
  100.         v[a[i]] = v[i];
  101.     for (int i = 0; i < n; ++i)
  102.         cout << v[i] << " ";
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement