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

📄 bubble.cpp

📁 不错书对C++/C程序员很有用的大家不要错过.
💻 CPP
字号:
#include <iostream.h>

template <class X> void bubble_sort(X *items, int size)
template <class X> void show_items(X *items, int size)

void main(void)
 {
   int iarray[7] =      {7, 5, 4, 3, 9, 8, 6};
   double darray[5] =   {4.2, 2.5, -0.9, 100.2, 3.0};

   cout << "Here is unsorted integer array: " << endl;
   show_items(iarray, 7);
   cout << "Here is unsorted double array: " << endl;
   show_items(darray, 5);
   bubble_sort(iarray, 7);
   bubble_sort(darray, 5);
   cout << "Here is sorted integer array: " << endl;
   show_items(iarray, 7);
   cout << "Here is sorted double array: " << endl;
   show_items(darray, 5);
 }

template <class X> void bubble_sort(X *items, int size)
 {
   register int i, j;
   X temp;

   for (i = 1; i < size; i++)
    for (j = size-1; j >= i; j--)
      if (items[j-1] > items[j])
        {
          temp = items[j-1];
          items[j-1] = items[j];
          items[j] = temp;
        }
  }

template <class X> void show_items(X *items, int size)
 {
   int i;

   for(i=0; i < size; i++)
      cout << items[i] << ", ";
   cout << endl;
 }


⌨️ 快捷键说明

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