带字符串的选择排序
Selection sort with strings
好的,我一直在使用这段代码对整数进行选择排序:
public void selectSort(int [] arr)
{
//pos_min is short for position of min
int pos_min,temp;
for (int i=0; i < arr.Length-1; i++)
{
pos_min = i; //set pos_min to the current index of array
for (int j=i+1; j < arr.Length; j++)
{
if (arr[j] < arr[pos_min])
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
但现在我想 运行 字符串列表上的相同算法。
这怎么可能实现呢?感觉真的很尴尬,就像你需要额外的循环来比较不同字符串的多个字符..?
我尝试了很多,但我想不出任何有用的东西。 :/
注意:
我知道,选择排序不是很有效。这仅用于学习目的。我不是在寻找替代算法或 类 已经是 C# 的一部分。 ;)
System.String
class 有一个静态 int Compare(string, string)
方法,如果第一个字符串小于第二个字符串,则 returns 为负数,如果它们相等则为零,如果第一个较大,则为正整数。
"smaller" 我的意思是它在词汇顺序中排在另一个之前,更大的意思是它在词汇顺序中排在另一个之后。
因此您可以比较 String.Compare(arr[j], arr[pos_min]) < 0
而不是仅仅 arr[j] < arr[pos_min]
整数。
IComparable
是一个接口,它为我们提供了一个名为 CompareTo 的函数,它是一个比较运算符。此运算符适用于实现 IComparable
接口的所有类型,其中包括整数和字符串。
// Forall types A where A is a subtype of IComparable
public void selectSort<A>(A[] arr)
where A : IComparable
{
//pos_min is short for position of min
int pos_min;
A temp;
for (int i=0; i < arr.Length-1; i++)
{
pos_min = i; //set pos_min to the current index of array
for (int j=i+1; j < arr.Length; j++)
{
// We now use 'CompareTo' instead of '<'
if (arr[j].CompareTo(arr[pos_min]) < 0)
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
我在python3.6
写代码
首先导入 sys 模块以使用我们语法中的各种功能。
import sys
考虑字符串数据类型项目的数组。
A = ['Chuck', 'Ana', 'Charlie', 'Josh']
for i in range(0, len(A)):
min_val = i
for j in range(i+1, len(A)):
if A[min_val] > A[j]:
min_val = j
在此处交换索引值和最小值。
(A[min_val], A[i]) = (A[i], A[min_val])
print("Sorted Array is :")
for i in range(len(A)):
print("%s" % A[i])
这对于字符串数据类型的数组非常有效,并按字母顺序对输入数据进行排序。
在输入中 'Charlie' 和 'Chuck' 正在根据他们的字母偏好进行比较,直到第 3 位,并相应地排列。
此程序在 python 控制台上的输出是
排序数组为:
安娜
查理
查克
乔什
好的,我一直在使用这段代码对整数进行选择排序:
public void selectSort(int [] arr)
{
//pos_min is short for position of min
int pos_min,temp;
for (int i=0; i < arr.Length-1; i++)
{
pos_min = i; //set pos_min to the current index of array
for (int j=i+1; j < arr.Length; j++)
{
if (arr[j] < arr[pos_min])
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
但现在我想 运行 字符串列表上的相同算法。
这怎么可能实现呢?感觉真的很尴尬,就像你需要额外的循环来比较不同字符串的多个字符..?
我尝试了很多,但我想不出任何有用的东西。 :/
注意: 我知道,选择排序不是很有效。这仅用于学习目的。我不是在寻找替代算法或 类 已经是 C# 的一部分。 ;)
System.String
class 有一个静态 int Compare(string, string)
方法,如果第一个字符串小于第二个字符串,则 returns 为负数,如果它们相等则为零,如果第一个较大,则为正整数。
"smaller" 我的意思是它在词汇顺序中排在另一个之前,更大的意思是它在词汇顺序中排在另一个之后。
因此您可以比较 String.Compare(arr[j], arr[pos_min]) < 0
而不是仅仅 arr[j] < arr[pos_min]
整数。
IComparable
是一个接口,它为我们提供了一个名为 CompareTo 的函数,它是一个比较运算符。此运算符适用于实现 IComparable
接口的所有类型,其中包括整数和字符串。
// Forall types A where A is a subtype of IComparable
public void selectSort<A>(A[] arr)
where A : IComparable
{
//pos_min is short for position of min
int pos_min;
A temp;
for (int i=0; i < arr.Length-1; i++)
{
pos_min = i; //set pos_min to the current index of array
for (int j=i+1; j < arr.Length; j++)
{
// We now use 'CompareTo' instead of '<'
if (arr[j].CompareTo(arr[pos_min]) < 0)
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
我在python3.6
写代码首先导入 sys 模块以使用我们语法中的各种功能。
import sys
考虑字符串数据类型项目的数组。
A = ['Chuck', 'Ana', 'Charlie', 'Josh']
for i in range(0, len(A)):
min_val = i
for j in range(i+1, len(A)):
if A[min_val] > A[j]:
min_val = j
在此处交换索引值和最小值。
(A[min_val], A[i]) = (A[i], A[min_val])
print("Sorted Array is :")
for i in range(len(A)):
print("%s" % A[i])
这对于字符串数据类型的数组非常有效,并按字母顺序对输入数据进行排序。
在输入中 'Charlie' 和 'Chuck' 正在根据他们的字母偏好进行比较,直到第 3 位,并相应地排列。
此程序在 python 控制台上的输出是
排序数组为: 安娜 查理 查克 乔什