C++二维数组排序

By admin at 2022-10-27 • 0人收藏 • 632人看过

image.png

#include <iostream>
#include<algorithm>

typedef struct student {
    int id;
    int math;
    int chinese;
    int english;
}student;

int main()
{
    student st[6] = {
        {3,5,34,34},
        {1,8,23,56},
        {2,45,78,24},
        {3,1,24,67},
        {5,1,35,86},
        {3,3,24,67},
    };
    //匿名表达式
    std::sort(st, st + 6, [](student s1, student s2) {
        if (s1.id == s2.id)
        {
            return s1.math<s2.math;
        }
        return s1.id < s2.id;
        });

    for (size_t i = 0; i < 6; i++)
    {
        std::cout << st[i].id << " : " << st[i].math << " : " << st[i].chinese << " : " << st[i].english << "\n";
    }

}


4 个回复 | 最后更新于 2022-10-31
2022-10-27   #1

vector动态数组排序

#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;

typedef struct student {
    int id;
    int math;
    int chinese;
    int english;
}student;

vector<student> st;

int main()
{
    st.push_back({ 3,5,34,34 });
    st.push_back({ 1,8,23,56 });
    st.push_back({ 2,45,78,24 });
    st.push_back({ 3,1,24,67 });
    st.push_back({ 5,1,35,86 });
    st.push_back({ 3,3,24,67 });
    for (size_t i = 0; i < st.size(); i++)
    {
        cout << st[i].id << " : " << st[i].math << " : " << st[i].chinese << " : " << st[i].english << "\n";
    }
    //匿名表达式
    sort(st.begin(), st.end(), [](student s1, student s2) {
        if (s1.id == s2.id)
        {
            return s1.math<s2.math;
        }
        return s1.id < s2.id;
        });
    cout << "-----------------------\n";
    for (size_t i = 0; i < st.size(); i++)
    {
        cout << st[i].id << " : " << st[i].math << " : " << st[i].chinese << " : " << st[i].english << "\n";
    }
}

image.png

2022-10-30   #2

最近上手了golang,瞬间觉得python不香了,虽然同样的代码python要简洁得多,但性能来说golang甩开python几个数量级了。


你的这个排序功能,用golang实现了一下:

package main

import (
   "encoding/json"
   "fmt"
   "sort"
)

func d2Sort(d [][]int) [][]int {
   nd := []string{}
   for _, arr := range d {
      sort.Ints(arr)
      jsbyte, _ := json.Marshal(arr)
      nd = append(nd, string(jsbyte))
   }
   sort.Strings(nd)
   nnd := [][]int{}
   for _, str := range nd {
      var arr []int
      err := json.Unmarshal([]byte(str), &arr)
      if err != nil {
         fmt.Println(err)
         break
      }
      nnd = append(nnd, arr)
   }
   return nnd
}

func main() {
   fmt.Println(d2Sort([][]int{
      {3, 5, 34, 34},
      {1, 8, 23, 56},
      {2, 45, 78, 24},
      {3, 1, 24, 67},
      {5, 1, 35, 86},
      {3, 3, 24, 67},
   }))
}

实现的结果是双重排序,先内部排序,排完后对外层再用哈希序列化后排序

C:\Users\Administrator\AppData\Local\Temp\GoLand\___go_build_main_go.exe
[[1 3 24 67] [1 5 35 86] [1 8 23 56] [2 24 45 78] [3 3 24 67] [3 5 34 34]]

如果仅需要外层排序的话,那更简单了,直接把内层转化json格式的哈希序列进行排序即可。

2022-10-30   #3

进一步,golang的优势其实是原生的多线程支持,所以对于这种排序问题,尤其是排序数量非常大的话,我们可以利用golang的多线程进行排序。

我对比了一下使用单线程和多线程的性能差异:

当数据量为一百万时(1000x1000的数组)

使用单线程排序1000x1000的数组,耗时  2477 ms
使用多线程排序1000x1000的数组,耗时  1795 ms
效率提升 37.99 %
package main

import (
   "encoding/json"
   "fmt"
   "math/rand"
   "sort"
   "time"
)

func chSort(arr []int, ch chan any) {
   sort.Ints(arr)
   ch <- arr
}

func d2Sort(d [][]int) [][]int {
   nd := []string{}
   ch := make(chan any, len(d))
   for _, arr := range d {
      go chSort(arr, ch)
   }
   for i := 0; i < len(d); i++ {
      out := <-ch
      if v, ok := out.([]int); ok {
         jsbyte, _ := json.Marshal(v)
         nd = append(nd, string(jsbyte))
      } else {
         fmt.Println(ok)
         return [][]int{}
      }

   }

   sort.Strings(nd)
   nnd := [][]int{}

   for _, str := range nd {
      var arr []int
      err := json.Unmarshal([]byte(str), &arr)
      if err != nil {
         fmt.Println(err)
         break
      }
      nnd = append(nnd, arr)
   }
   return nnd
}

func d2Sort2(d [][]int) [][]int {
   nd := []string{}
   for _, arr := range d {
      sort.Ints(arr)
      jsbyte, _ := json.Marshal(arr)
      nd = append(nd, string(jsbyte))
   }
   sort.Strings(nd)
   nnd := [][]int{}
   for _, str := range nd {
      var arr []int
      err := json.Unmarshal([]byte(str), &arr)
      if err != nil {
         fmt.Println(err)
         break
      }
      nnd = append(nnd, arr)
   }
   return nnd
}

func main() {
   test2d := [][]int{}
   for i := 0; i < 10000; i++ {
      row := []int{}
      for j := 0; j < 1000; j++ {
         row = append(row, rand.Int())
      }
      test2d = append(test2d, row)
   }
   st2 := time.Now().UnixMilli()
   d2Sort2(test2d)
   ed2 := time.Now().UnixMilli()
   d2 := ed2 - st2
   fmt.Println("使用单线程排序1000x1000的数组,耗时 ", d2, "ms")
   st := time.Now().UnixMilli()
   d2Sort(test2d)
   ed := time.Now().UnixMilli()
   d1 := ed - st
   fmt.Println("使用多线程排序1000x1000的数组,耗时 ", d1, "ms")
   fmt.Printf("效率提升 %.2f %%", float64(d2-d1)/float64(d1)*100)
}


2022-10-31   #4

跟不上组织节奏 了.....

谢谢jerryxjr分享

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...