⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a605bubl.cpp

📁 quarto esempi vari per c++ (schemi base)
💻 CPP
字号:
// nuovi concetti: algoritmo di ordinamento "Bubble Sort"
#include <iostream>

using namespace std;

int main() {
  const int n=400;
  int i, j, sup, ultimo_scambiato, t, a[n];

  srand(time(0));
  for (i=0; i<n; i++)  a[i]=i+1;
  for (i=0; i<n; i++) {
    t = a[i];
    j = rand()%n;
    a[i] = a[j];
    a[j] = t;
  }
  for (i=0; i<n; i++)
    cout << a[i] << (i%20 == 19 ? "\n" : " ");
  cout << "\nInizio ordinamento\n";
  sup = n-1;
  while (sup >= 0) {
    ultimo_scambiato = -1;
    for (i=0; i<sup; i++)
      if (a[i]>a[i+1]) {
	t = a[i];
	a[i] = a[i+1];
	a[i+1] = t;
	ultimo_scambiato = i;
      }
    sup = ultimo_scambiato;
  }
  cout << "Fine ordinamento.  Premere Invio...\n";
  cin.get();
  for (i=0; i<n; i++)
    cout << a[i] << (i%20 == 19 ? "\n" : " ");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -