selctcon.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 55 行

CPP
55
字号
 
template <class Record>
void Sortable_list<Record>::swap(int low, int high)
/* 
 
Pre:  low and high are valid positions in the Sortable_list.
Post: The entry at position low is swapped with the entry at position high.
Uses: The contiguous List implementation of ?list_ch?.
 
*/
{
   Record temp;
   temp = entry[low];
   entry[low] = entry[high];
   entry[high] = temp;
}
 
template <class Record>
int Sortable_list<Record>::max_key(int low, int high)
/* 
 
Pre:  low and high are valid positions in the Sortable_list and
     low <= high.
Post: The position of the entry between low and high with the largest
      key is returned.
Uses: The class Record.
      The contiguous List implementation of ?list_ch?.
 
*/
{
   int largest, current;
   largest = low;
   for (current = low + 1; current <= high; current++)
      if (entry[largest] < entry[current])
         largest = current;
   return largest;
}
 
template <class Record>
void Sortable_list<Record>::selection_sort()
/* 
 
Post: The entries of the Sortable_list
have been rearranged so that the keys in all the
entries are sorted into nondecreasing order.
Uses: max_key, swap.
 
*/
{
   for (int position = count - 1; position > 0; position--) {
      int max = max_key(0, position);
      swap(max, position);
   }
}

⌨️ 快捷键说明

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