选择排序

// 选择排序.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

#include <iostream>
#include<random>
#include <iomanip>
using namespace std;

typedef struct slist
{
    int data;
    slist *next;
}nodelist,*sqlist;


void createlist(sqlist p, int n)
{
    int i = n;
    random_device rd;
    mt19937 mt(rd());
    while (n--)
    {
        sqlist q;
        q = new nodelist;
        q->data = mt()%1000;
        q->next = NULL;
        p->next = q;
        p = p->next;
    }
}
void print(sqlist p, int n)
{
    while (n--)
    {
        cout <<left<<setw(3)<< p->data <<"   ";
        p=p->next;
    }
    cout << endl;
}
void select(sqlist p)
{
    int t;
    sqlist q1, q2,q;
    q1 =  p;
    while (q1)
    {
        q2 = q1;
        q = q1;
        while (q2)
        {
            if (q2->data < q->data)
                q = q2;
            q2 = q2->next;
        }
        if (q != q1)
        {
            t = q1->data;
            q1->data = q->data;
            q->data = t;
        }
        q1 = q1->next;
    }
    
}

int main()
{
    sqlist head;
    int n;

    head = new nodelist;
    head->next = NULL;
    head->data = 0;

    cout << "请输入预计排序数据的个数:";
    cin >> n;

    createlist(head,n);
    cout << "开始获取随机数组!" << endl;
    print(head->next, n);
    cout << "输出排序后的随机数组!" << endl;
    select(head->next);
    print(head->next, n);
    return 0;

}

  

上一篇:【LeetCode】213. 打家劫舍 II


下一篇:213. 打家劫舍 II