使用选择排序方法对二维数组进行排序并将结果写入文件
Sort 2D array with selection-sort method and write the result to the file
我有任务要做,我有点卡住了。
我必须使用 'selection sort' 方法从文件中订购一些数组,并将解决方案写入文件末尾。
例如:
file.txt
9 3 1 12 8 6
22 3 1 8
78 61 19 5 99
给出。排序后我应该有这样的东西:
file.txt
9 3 1 12 8 6
22 3 1 8
78 61 19 5 99
----sorted----
1 3 6 8 9 12
1 3 8 22
5 19 61 78 99
我不得不提一下,我必须在 PHP、Java、C# 和 Python 中执行此操作。
我开始在 PHP 中编码,但我有点卡住了。我的代码如下所示:
<?php
function selectionSort(array $array) {
$length = count($array);
for($i = 0; $i < $length; $i ++) {
$min = $i;
for($j = $i + 1; $j < $length; $j ++) {
if ($array[$j] < $array[$min]) {
$min = $j;
}
}
$tmp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $tmp;
}
return $array;
}
//CREATE ARRAYS FROM FILE LINES:
$file_handle = fopen("fisier.txt", "r+");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
$parts_sorted = selectionSort($parts);
for ($n=0; $n<count($parts_sorted); $n++){
echo $parts_sorted[$n]." ";
}
echo "<br>";
}
fclose($file_handle);
这里的问题是排序不好,我是说我的txt文件是
9 12 5 4 13 8
3 7 12 44 22 4 13
70 1 12 55 34
22 13 7 50 3 1 9 14 27 77
56 2 9 45 35 12 7 63
php 代码的结果是
4 5 8 9 12 13
13 3 4 7 12 22 44
1 12 34 55 70
1 3 7 77 9 13 14 22 27 50
2 7 9 12 35 45 56 63
这不是一个好的排序。你能说出为什么吗?
你知道我接下来应该怎么做才能将排序后的数组写入文件末尾吗?
对 java、c# 和 python 的一点帮助会很棒。很抱歉 post 但我是这里的菜鸟。谢谢!
fgets() returns 结束该行的 linefeed/carriage-return 字符
所以你不是在比较 13
而是 13\r\n
或 13\n
或 ....
你可以做类似的事情
$line_of_text = trim(fgets($file_handle));
去除从文件 and/or
中读取的行的任何 leading/trailing 空格
$parts = array_map('intval', explode(' ', $line_of_text));
转换为every element to an integer.
(如果所有元素都在 php 整数的值范围内,我建议使用 array_map/intval thingy 作为数字的比较 as number 显然是你想要的。否则你 可能 对 strnatcmp() 作为你的比较函数感兴趣而不是 < operator)
这是一个简短的 Java 片段。
它的作用
- 逐行读取文件
file.txt
- 将一行的值转换为整数值
- 使用方法
selectionSort(int[])
对整数值进行排序
- 将排序后的值写回文件
file_sorted.txt
.
// equal to your PHP implementation
static void selectionSort(int[] ints) {
for (int i = 0; i < ints.length - 1; i++) {
int min = i;
for (int j = i + 1; j < ints.length; j++) {
if (ints[j] < ints[min]) {
min = j;
}
}
int temp = ints[min];
ints[min] = ints[i];
ints[i] = temp;
}
}
...
Path fileIn = Paths.get("file.txt");
Path fileOut = Paths.get("file_sorted.txt");
try (BufferedReader br = Files.newBufferedReader(fileIn);
BufferedWriter bw = Files.newBufferedWriter(fileOut,
StandardOpenOption.CREATE_NEW)) {
for (String l = br.readLine(); l != null; l = br.readLine()) {
String[] fields = l.split(" +");
int[] ints = Arrays.asList(fields) // convert to List
// convert the List into a Stream
.stream()
// map the entries to their integer values
.mapToInt(f -> Integer.valueOf(f))
// convert the stream to an array
.toArray();
selectionSort(ints);
for (int i = 0; i < ints.length; i++) {
bw.append(Integer.toString(ints[i]));
if (i < ints.length - 1) {
bw.append(' ');
}
}
bw.newLine();
}
}
输入file.txt
9 12 5 4 13 8
3 7 12 44 22 4 13
70 1 12 55 34
22 13 7 50 3 1 9 14 27 77
56 2 9 45 35 12 7 63
输出file_sorted.txt
4 5 8 9 12 13
3 4 7 12 13 22 44
1 12 34 55 70
1 3 7 9 13 14 22 27 50 77
2 7 9 12 35 45 56 63
我有任务要做,我有点卡住了。 我必须使用 'selection sort' 方法从文件中订购一些数组,并将解决方案写入文件末尾。 例如:
file.txt
9 3 1 12 8 6
22 3 1 8
78 61 19 5 99
给出。排序后我应该有这样的东西:
file.txt
9 3 1 12 8 6
22 3 1 8
78 61 19 5 99
----sorted----
1 3 6 8 9 12
1 3 8 22
5 19 61 78 99
我不得不提一下,我必须在 PHP、Java、C# 和 Python 中执行此操作。 我开始在 PHP 中编码,但我有点卡住了。我的代码如下所示:
<?php
function selectionSort(array $array) {
$length = count($array);
for($i = 0; $i < $length; $i ++) {
$min = $i;
for($j = $i + 1; $j < $length; $j ++) {
if ($array[$j] < $array[$min]) {
$min = $j;
}
}
$tmp = $array[$min];
$array[$min] = $array[$i];
$array[$i] = $tmp;
}
return $array;
}
//CREATE ARRAYS FROM FILE LINES:
$file_handle = fopen("fisier.txt", "r+");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
$parts_sorted = selectionSort($parts);
for ($n=0; $n<count($parts_sorted); $n++){
echo $parts_sorted[$n]." ";
}
echo "<br>";
}
fclose($file_handle);
这里的问题是排序不好,我是说我的txt文件是
9 12 5 4 13 8
3 7 12 44 22 4 13
70 1 12 55 34
22 13 7 50 3 1 9 14 27 77
56 2 9 45 35 12 7 63
php 代码的结果是
4 5 8 9 12 13
13 3 4 7 12 22 44
1 12 34 55 70
1 3 7 77 9 13 14 22 27 50
2 7 9 12 35 45 56 63
这不是一个好的排序。你能说出为什么吗? 你知道我接下来应该怎么做才能将排序后的数组写入文件末尾吗? 对 java、c# 和 python 的一点帮助会很棒。很抱歉 post 但我是这里的菜鸟。谢谢!
fgets() returns 结束该行的 linefeed/carriage-return 字符
所以你不是在比较 13
而是 13\r\n
或 13\n
或 ....
你可以做类似的事情
$line_of_text = trim(fgets($file_handle));
去除从文件 and/or
中读取的行的任何 leading/trailing 空格$parts = array_map('intval', explode(' ', $line_of_text));
转换为every element to an integer.
(如果所有元素都在 php 整数的值范围内,我建议使用 array_map/intval thingy 作为数字的比较 as number 显然是你想要的。否则你 可能 对 strnatcmp() 作为你的比较函数感兴趣而不是 < operator)
这是一个简短的 Java 片段。
它的作用
- 逐行读取文件
file.txt
- 将一行的值转换为整数值
- 使用方法
selectionSort(int[])
对整数值进行排序
- 将排序后的值写回文件
file_sorted.txt
.
// equal to your PHP implementation
static void selectionSort(int[] ints) {
for (int i = 0; i < ints.length - 1; i++) {
int min = i;
for (int j = i + 1; j < ints.length; j++) {
if (ints[j] < ints[min]) {
min = j;
}
}
int temp = ints[min];
ints[min] = ints[i];
ints[i] = temp;
}
}
...
Path fileIn = Paths.get("file.txt");
Path fileOut = Paths.get("file_sorted.txt");
try (BufferedReader br = Files.newBufferedReader(fileIn);
BufferedWriter bw = Files.newBufferedWriter(fileOut,
StandardOpenOption.CREATE_NEW)) {
for (String l = br.readLine(); l != null; l = br.readLine()) {
String[] fields = l.split(" +");
int[] ints = Arrays.asList(fields) // convert to List
// convert the List into a Stream
.stream()
// map the entries to their integer values
.mapToInt(f -> Integer.valueOf(f))
// convert the stream to an array
.toArray();
selectionSort(ints);
for (int i = 0; i < ints.length; i++) {
bw.append(Integer.toString(ints[i]));
if (i < ints.length - 1) {
bw.append(' ');
}
}
bw.newLine();
}
}
输入file.txt
9 12 5 4 13 8
3 7 12 44 22 4 13
70 1 12 55 34
22 13 7 50 3 1 9 14 27 77
56 2 9 45 35 12 7 63
输出file_sorted.txt
4 5 8 9 12 13
3 4 7 12 13 22 44
1 12 34 55 70
1 3 7 9 13 14 22 27 50 77
2 7 9 12 35 45 56 63