简体   繁体   English

C++插入排序

[英]C++ insertion sort

I have this function called WordSort(worddata W [], int count) that is fed two variables 1 - worddata is the array holding information on a given word in a file.我有这样的功能称为WordSort(worddata W [],INT计数)被供给两个变量1 - worddata是在一个文件中的给定字的阵列保持信息。 count is just the counter variable to see which word in the array we are looking at.计数只是计数器变量,看看我们正在寻找这词在数组中。

the words.txt file that is read into this program would just be a string of words.被读入该程序words.txt文件也只是一串字。

this is a list of words
there are letters and numbers
23 people recommend this program.

Heres the function:继承人的功能:

void WordSort (worddata W [], int count)
{
  for (int i=1; i < count; i++)
         {
           for (int j=i; j > 0 && W[j-1].word > W[j].word; j--)
             {
               Swap(W[j], W[j-1]);
             }
         }
}

The swap function is suppose to swap every element with the one before it as long as j > 0 or the list is over.交换函数是假设之前它只要与所述一个的每一个元素交换为J> 0或列表结束。 Im confused on how to complete the swap function, here's the example i was given.我对如何完成交换功能感到困惑,这是我给出的示例。

void Swap (worddata & a, worddata & b)
{
 int += a;
 a = b;
 b =+;
}

Swap is suppose to swap every element with the one before it交换是假设每一个元素交换与之前的一个

I think the WordSort function works fine, the only thing missing is the Swap function.我认为WordSort功能工作正常,唯一缺少的是交换功能。 Could anyone point me in the right direction or explain insertion sorting better to me?任何人都可以点我在正确的方向或解释插入排序好给我?

void insertion_sort()
{


    /* Algorithm : Insertion Sort
     * Coded by .
    */
    int num;
    /*
     * Asking the User no of Integers he/she wants to enter
     */
    cout << "Enter no of integers u want to enter: ";
    cin >> num;
    /* Creating an Array to store the integers*/
    int s[num];
    /*Taking Integers from the User */
    for(int i = 0 ; i < num ; i++)
    {
        cout << "Integer " << i+1 << " is : ";
        int x;
        cin >> x;
        s[i] = x;
    }
    /* The Magic of INSERTION SORT */
    for(int j = 1 ; j <= (num-1) ; j++)
    {
        int key = s[j]; 
        int k = j-1;

        while(k >=0 && key <= s[k])
        {
            s[k+1] = s[k];
            k = k - 1;
        }
        s[k+1]=key;

    }
    /*Printing Out the Sorted List */
    cout << "The Sorted List is \n\n";
    for(int i = 0 ; i < num ; i++)
    {
        cout << s[i] << "  ";
    }

}

Use standard library std::swap instead.改用标准库std::swap In your loop:在你的循环中:

for (...)
{
    std:swap(W[j], W[j-1]);
}

std::swap requires worddata class to have a copy constructor and an assignment operator defined explicitly or implicitly. std::swap 要求 worddata 类具有显式或隐式定义的复制构造函数和赋值运算符。

Swap should look like this -- I have no idea how your example is even close. Swap 应该是这样的——我不知道你的例子有多接近。

void Swap (worddata & a, worddata & b)
{
 worddata temp = a;
 a = b;
 b = temp;
}

Insertion sort using "for loop" (2 iterations)使用“for循环”的插入排序(2次迭代)

#include<iostream>
using namespace std;


int insertion(int arr[], int size_arr)
{
    int i,j,n, temp;

    for(i=1;i<size_arr; i++){
            j=i-1;
            temp = arr[i];
            for (j; j >=  0; j--)
        {
            if(arr[j] > temp){
                        arr[j+1] = arr[j];
                        arr[j] = temp;
            }
        }
            arr[j] = temp;
      }

    for(i=0;i<size_arr;i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}

int main(){
    int arr[] = {3,38,1,44,66,23,105,90,4,6};
    int size_arr = sizeof(arr) / sizeof(arr[0]);
    insertion(arr,size_arr);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM