我有一个结构 ElementType
typedef struct
{
int AtomicNumber;
char Name[31];
char Symbol[4];
} ElementType;
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现一种排序算法,该算法将按字母顺序对元素进行排序。我比较了字符串,但没有任何效果。我无法弄清楚我下面的功能有什么问题。
void sortAlphabetical(ElementType elements[NUM_ELEMENTS])
{
printf("SORTING!\n");
int c, d;
for (c = 0 ; c < NUM_ELEMENTS - 1; c++)
{
for (d = 0 ; d < NUM_ELEMENTS - c - 1; d++)
{
if (elements[d].Name > elements[d+1].Name)
{
ElementType temp;
temp.AtomicNumber = elements[d].AtomicNumber;
strcpy(temp.Name, elements[d].Name);
strcpy(temp.Symbol, elements[d].Symbol);
elements[d].AtomicNumber = elements[d+1].AtomicNumber;
strcpy(elements[d].Name, elements[d+1].Name);
strcpy(elements[d].Symbol, elements[d+1].Symbol);
elements[d+1].AtomicNumber = temp.AtomicNumber;
strcpy(elements[d+1].Name, temp.Name);
strcpy(elements[d+1].Symbol, temp.Symbol);
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个列表,其中元素已经排序。我想过滤掉大于给定数字的元素。
例如,
original_list = [2,3,5,7,11]
limit = 6
expected_list = [2,3,5] # All elements <=6
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最有效方法是什么?
我看到的所有现有问题都处理未排序的列表。如果列表已经排序,我相信会有更有效的过滤方式。
我拥有的数据包括两个变量:id和income(字符列表)
id <- seq(1,6)
income <- c("2322;5125",
"0110;2012",
"2212;0912",
"1012;0145",
"1545;1102",
"1010;2028")
df <- data.frame(id, income)
df$income <- as.character(df$income)
Run Code Online (Sandbox Code Playgroud)
我需要添加第三列income_order,其中包括列收入的有序值。最终输出看起来像
注意:我仍然需要保留前导零
我有一个游戏列表:
data Game = Game {
round ::Int
, time :: Maybe ZonedTime
, location :: String
, hometeam :: String
, awayteam :: String
, result :: Maybe (Int, Int)
} deriving (Show)
type Games = [Game]
Run Code Online (Sandbox Code Playgroud)
如何根据时间属性对游戏列表进行排序?
谢谢!
问题:有n个球,标记为0、1、2,顺序乱,我想从小到大排序。球:
1, 2, 0, 1, 1, 2, 2, 0, 1, 2, ...
Run Code Online (Sandbox Code Playgroud)
一定要用最快的方式来解决,不能用sort()函数,我想了很多办法,比如冒泡排序,插入排序等等,但是速度不快。是否有一种算法使时间复杂度为 O(logn) 或 O(n)?
给定的球列表 A[] 和长度 n
void sortBalls(int A[], int n)
{
//code here
}
Run Code Online (Sandbox Code Playgroud) 假设我有三个排序的数组
答:{ 4, 9 }
乙:{2, 11}
C : { 12, 14}
那么三元组 { x, y, z } 的数量,使得 x 属于 A,y 属于 B,z 属于 C,使得 x < y < z 是 -> 4
我知道 O( n ^3 ) 算法,但如何在 O(n) 中做到。其中 n 是数组的长度。
我的文件中的行内容格式如下:
Ref.,ID,Firstname,Secondname,City
Run Code Online (Sandbox Code Playgroud)
这是我的文件:
1,4234,Firstname1,Secondname1,City1
2,5647,Firstname2,Secondname2,City2
3,1657,Firstname3,Secondname3,City3
4,3898,Firstname4,Secondname4,City4
Run Code Online (Sandbox Code Playgroud)
现在我想按 ID 对其进行排序,然后将其再次放入文件中,如下所示:
3,1657,Firstname3,Secondname3,City3
4,3898,Firstname4,Secondname4,City4
1,4234,Firstname1,Secondname1,City1
2,5647,Firstname2,Secondname2,City2
Run Code Online (Sandbox Code Playgroud)
注意:整行是一个字符串变量。
那么有没有办法做到这一点?
这是我的代码:
int counter = 0;
string ln; //Each line in file per loop Ex:1,8957,Firstname,lastname,city
using (StreamReader file = new StreamReader(filePath))
{
Console.WriteLine("Records in the file" + Environment.NewLine);
Console.WriteLine("Ref,First name,Second name,City" + Environment.NewLine);
while ((ln = file.ReadLine()) != null)
{
Console.WriteLine(ln + Environment.NewLine);
/*
So here i need to sort the lines in the file, which written hard coded in the file before spliting it. …Run Code Online (Sandbox Code Playgroud) 给定一些扩展共同特征的案例类,我希望能够根据它们的类型定义一个排序(实际上不一定,目标是以这种方式对它们进行排序)。例如:
sealed trait Element
case class A(x: Int) extends Element
case class B(x: Int) extends Element
case class C(x: Int) extends Element
case class D(x: Int) extends Element
case class E(x: Int) extends Element
case class F(x: Int) extends Element
val elements: List[Element] = List(
A(5), F(3), E(1), C(19), A(3), F(1)
)
Run Code Online (Sandbox Code Playgroud)
排序为F -> A -> all other cases,因此结果列表为List(F(3), F(1), A(5), A(3), E(1), C(19))。相同类型元素之间的顺序无关紧要。
我想出了多种不同的解决方案,但它们看起来都很复杂,我只是觉得我错过了一些明显的方法来实现这一点。这就是我使用排序实现它的方式:
val sorted = elements.sorted{(a: Element, b: Element) => (a, b) match …Run Code Online (Sandbox Code Playgroud) sorting functional-programming scala pattern-matching case-class
我有一个清单
a=[2,7,23,4563,23].
Run Code Online (Sandbox Code Playgroud)
如何在一行中打印所有元素的索引?
基本上,我必须对列表进行排序并打印原始索引作为输出。例子:
A=[4,5,3,7,1]
Run Code Online (Sandbox Code Playgroud)
排序后新数组变为
A=[1,3,4,5,7]
Run Code Online (Sandbox Code Playgroud)
所需的输出应该是
4 2 0 1 3
Run Code Online (Sandbox Code Playgroud) 我有以下锯齿状阵列-
int[][] triangle =
{
new int[] { 3 },
new int[] { 7,4 },
new int[] { 2,4,6 },
new int[] { 8,5,9,3 }
};
Run Code Online (Sandbox Code Playgroud)
然后我复制了它-
int[][] temporaryTriangle = new int[triangle.Length][];
Array.Copy(triangle, temporaryTriangle, triangle.Length);
Run Code Online (Sandbox Code Playgroud)
我对副本的子数组进行了排序-
for (int i = 0; i < temporaryTriangle.Length; i++)
{
Array.Sort(temporaryTriangle[i]);
}
Run Code Online (Sandbox Code Playgroud)
我发现源数组的子数组也被排序了!我的问题是为什么会发生这种情况,我的意思是为什么当我对副本的子数组进行排序时,数组的子数组会被排序?
用法:
foreach (var subarray in triangle)
Console.WriteLine(string.Join(" ", subarray));
foreach (var subarray in temporaryTriangle)
Console.WriteLine(string.Join(" ", subarray));
// Output:
// 3
// 4 7
// 2 4 6
// 3 5 …Run Code Online (Sandbox Code Playgroud)