C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

ノームソート用のC++プログラム?


ここでは、ノームソートがどのように機能するかを見ていきます。これは別の並べ替えアルゴリズムです。このアプローチでは、リストがすでにソートされている場合、O(n)時間がかかります。したがって、最良の場合の時間計算量はO(n)です。しかし、平均的なケースと最悪のケースの複雑さはO(n ^ 2)です。ここで、この並べ替え手法についてのアイデアを得るためのアルゴリズムを見てみましょう。

アルゴリズム

gnomeSort(arr、n)

begin
   index := 0
   while index < n, do
      if index is 0, then
         index := index + 1
      end if
      if arr[index] >= arr[index -1], then
         index := index + 1
      else
         exchange arr[index] and arr[index - 1]
         index := index - 1
      end if
   done
end

#include<iostream>
using namespace std;
void gnomeSort(int arr[], int n){
   int index = 0;
   while(index < n){
      if(index == 0) index++;
      if(arr[index] >= arr[index - 1]){ //if the element is greater than previous one
         index++;
      } else {
         swap(arr[index], arr[index - 1]);
         index--;
      }
   }
}
main() {
   int data[] = {54, 74, 98, 154, 98, 32, 20, 13, 35, 40};
   int n = sizeof(data)/sizeof(data[0]);
   cout << "Sorted Sequence ";
   gnomeSort(data, n);
   for(int i = 0; i <n;i++){
      cout << data[i] << " ";
   }
}

出力

Sorted Sequence 13 20 32 35 40 54 74 98 98 154

  1. QuickSort用のC++プログラム?

    クイックソートは、比較を使用してソートされていないリスト(配列)をソートするソート手法です。クイックソートは、パーティション交換ソートとも呼ばれます。 等しいソート項目の相対的な順序が保持されないため、安定したソートではありません。クイックソートは配列を操作できるため、ソートを実行するために少量の追加メモリが必要です。常に最悪の場合のパーティションを選択するわけではないことを除いて、選択ソートと非常によく似ています。したがって、選択ソートのより適切な形式と見なすことができます。 QuickSortは、最も効率的な並べ替えアルゴリズムの1つであり、配列を小さい配列に分割することに基づいていま

  2. ノームソート用のPythonプログラム

    この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −配列が与えられたので、ノームソートを使用してソートする必要があります。 アルゴリズム 1. Firstly we traverse the array from left to right. 2. Now,if the current element is larger or equal to the previous element then we traverse one step ahead 3. otherwise,if the current element is smaller than the