原文:
给定 n 个字符串的数组。任务是按排序顺序打印字符串。方法应该是在排序过程中不应该将任何字符串复制到另一个字符串。
示例:
input : {"geeks", "for", "geeks", "quiz")
output : for geeks geeks quiz
input : {"ball", "pen", "apple", "kite"}
output : apple ball kite pen
进场:有以下步骤:
- 维护另一个数组 indexed_arr ,该数组存储/维护每个字符串的索引。
- 我们可以对这个 indexed_arr 应用任何排序技术。
插图:
--> str[] = {"world", "hello"}
--> corresponding index array will be
indexed_arr = {0, 1}
--> now, how the strings are compared and
accordingly values in indexed_arr are changed.
--> comparison process:
if (str[index[0]].compare(str[index[1]] > 0
temp = index[0]
index[0] = index[1]
index[1] = temp
// after sorting values of
// indexed_arr = {1, 0}
--> for i=0 to 1
print str[index[i]]
this is how the strings are compared and their
corresponding indexes in the indexed_arr
are being manipulated/swapped so that after the sorting process
is completed, the order of indexes in the indexed_arr
gives us the sorted order of the strings.
c
// c implementation to print array of strings in sorted
// order without copying one string into another
#include
using namespace std;
// function to print strings in sorted order
void printinsortedorder(string arr[], int n)
{
int index[n];
int i, j, min;
// initially the index of the strings
// are assigned to the 'index[]'
for (i=0; i 0)
min = j;
}
// index of the smallest string is placed
// at the ith index of 'index[]'
if (min != i)
{
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
// printing strings in sorted order
for (i=0; i
java 语言(一种计算机语言,尤用于创建网站)
//java implementation to print array of strings in sorted
// order without copying one string into another
class gfg {
// function to print strings in sorted order
static void printinsortedorder(string arr[], int n) {
int index[] = new int[n];
int i, j, min;
// initially the index of the strings
// are assigned to the 'index[]'
for (i = 0; i < n; i ) {
index[i] = i;
}
// selection sort technique is applied
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j ) {
// with the help of 'index[]'
// strings are being compared
if (arr[index[min]].compareto(arr[index[j]]) > 0) {
min = j;
}
}
// index of the smallest string is placed
// at the ith index of 'index[]'
if (min != i) {
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
// printing strings in sorted order
for (i = 0; i < n; i ) {
system.out.print(arr[index[i]] " ");
}
}
// driver program to test above
static public void main(string[] args) {
string arr[] = {"geeks", "quiz", "geeks", "for"};
int n = 4;
printinsortedorder(arr, n);
}
}
// this code is contributed by 29ajaykumar
python 3
# python 3 implementation to print array
# of strings in sorted order without
# copying one string into another
# function to print strings in sorted order
def printinsortedorder(arr, n):
index = [0] * n
# initially the index of the strings
# are assigned to the 'index[]'
for i in range(n):
index[i] = i
# selection sort technique is applied
for i in range(n - 1):
min = i
for j in range(i 1, n):
# with the help of 'index[]'
# strings are being compared
if (arr[index[min]] > arr[index[j]]):
min = j
# index of the smallest string is placed
# at the ith index of 'index[]'
if (min != i):
index[min], index[i] = index[i], index[min]
# printing strings in sorted order
for i in range(n):
print(arr[index[i]], end = " ")
# driver code
if __name__ == "__main__":
arr = ["geeks", "quiz", "geeks", "for"]
n = 4
printinsortedorder(arr, n)
# this code is contributed by ita_c
c
//c# implementation to print an array of strings in sorted
// order without copying one string into another
using system;
public class gfg {
// function to print strings in sorted order
static void printinsortedorder(string []arr, int n) {
int []index = new int[n];
int i, j, min;
// initially the index of the strings
// are assigned to the 'index[]'
for (i = 0; i < n; i ) {
index[i] = i;
}
// selection sort technique is applied
for (i = 0; i < n - 1; i ) {
min = i;
for (j = i 1; j < n; j ) {
// with the help of 'index[]'
// strings are being compared
if (arr[index[min]].compareto(arr[index[j]]) > 0) {
min = j;
}
}
// index of the smallest string is placed
// at the ith index of 'index[]'
if (min != i) {
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
// printing strings in sorted order
for (i = 0; i < n; i ) {
console.write(arr[index[i]] " ");
}
}
// driver program to test above
static public void main() {
string []arr = {"geeks", "quiz", "geeks", "for"};
int n = 4;
printinsortedorder(arr, n);
}
}
// this code is contributed by 29ajaykumar
java 描述语言
输出:
for geeks geeks quiz
时间复杂度: o(n 2 )
当我们必须最小化盘写入的数量时,该方法可以有它的用途,就像在结构阵列的情况下一样。结构值被比较,但是它们的值没有被交换,相反,它们的索引被保持在另一个数组中,该数组被操作来保持索引以表示结构的排序数组的顺序。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处