给定一个由字符的数组arr []和一个整数M ,任务是根据ASCII%M对数组进行排序,即,应首先显示ASCII值%M最小的字符。
例子:
Input: arr[] = {‘a’, ‘b’, ‘c’, ‘e’}, M = 2
Output: b a c e
The ASCII % M for the array are
{97 % 2, 98 % 2, 99 % 2, 101 % 2} i.e. {1, 0, 1, 1}
Input: arr[] = {‘g’, ‘e’, ‘e’, ‘k’, ‘s’}, M = 8
Output: k s e e g
方法1:编写一个对数组进行排序的函数,而不是比较字符的值,而是比较它们的ASCII值%M对数组进行排序。最后打印排序后的数组。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// A utility function to swap two elements
void swap(char* a, char* b)
{
char t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition(char arr[], int low, int high, int mod)
{
// pivot
char pivot = arr[high];
// Index of smaller element
int i = (low - 1);
int piv = pivot % mod;
for (int j = low; j <= high - 1; j++) {
int a = arr[j] % mod;
// If current element is smaller than or
// equal to pivot
// Instead of values, ASCII % m values
// are compared
if (a <= piv) {
// Increment index of smaller element
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(char arr[], int low, int high, int mod)
{
if (low < high) {
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high, mod);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1, mod);
quickSort(arr, pi + 1, high, mod);
}
}
// Function to print the given array
void printArray(char arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
char arr[] = { 'g', 'e', 'e', 'k', 's' };
int n = sizeof(arr) / sizeof(arr[0]);
int mod = 8;
// Sort the given array
quickSort(arr, 0, n - 1, mod);
// Print the sorted array
printArray(arr, n);
return 0;
}
Java // Java implementation of the approach
class GFG
{
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
static int partition(char arr[], int low, int high, int mod)
{
// pivot
char pivot = arr[high];
// Index of smaller element
int i = (low - 1);
int piv = pivot % mod;
for (int j = low; j <= high - 1; j++)
{
int a = arr[j] % mod;
// If current element is smaller than or
// equal to pivot
// Instead of values, ASCII % m values
// are compared
if (a <= piv)
{
// Increment index of smaller element
i++;
// swap
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
char t = arr[i+1];
arr[i+1] = arr[high];
arr[high] = t;
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void quickSort(char arr[], int low, int high, int mod)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high, mod);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1, mod);
quickSort(arr, pi + 1, high, mod);
}
}
// Function to print the given array
static void printArray(char arr[], int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
}
// Driver code
public static void main(String [] args)
{
char arr[] = { 'g', 'e', 'e', 'k', 's' };
int n = arr.length;
int mod = 8;
// Sort the given array
quickSort(arr, 0, n - 1, mod);
// Print the sorted array
printArray(arr, n);
}
}
// This code is contibuted by ihritik
C#
// C# implementation of the approach
using System;
class GFG
{
/* This function takes last element as pivot, places
the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
static int partition(char []arr, int low, int high, int mod)
{
// pivot
char pivot = arr[high];
// Index of smaller element
int i = (low - 1);
char t;
int piv = pivot % mod;
for (int j = low; j <= high - 1; j++)
{
int a = arr[j] % mod;
// If current element is smaller than o
// equal to pivot
// Instead of values, ASCII % m values
// are compared
if (a <= piv)
{
// Increment index of smaller element
i++;
// swap
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
t = arr[i+1];
arr[i+1] = arr[high];
arr[high] = t;
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
static void quickSort(char []arr, int low, int high, int mod)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high, mod);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1, mod);
quickSort(arr, pi + 1, high, mod);
}
}
// Function to print the given array
static void printArray(char []arr, int size)
{
for (int i = 0; i < size; i++)
Console.Write(arr[i] + " ");
}
// Driver code
public static void Main()
{
char []arr = { 'g', 'e', 'e', 'k', 's' };
int n = arr.Length;
int mod = 8;
// Sort the given array
quickSort(arr, 0, n - 1, mod);
// Print the sorted array
printArray(arr, n);
}
}
// This code is contibuted by ihritik
Python3
# Python3 implementation of the above approach
""" This function takes last element as pivot,
places the pivot element at its correct position
in sorted array, and places all smaller
(smaller than pivot) to left of pivot and
all greater elements to right of pivot """
def partition(arr, low, high, mod) :
# pivot
pivot = ord(arr[high]);
# Index of smaller element
i = (low - 1);
piv = pivot % mod;
for j in range(low, high) :
a = ord(arr[j]) % mod;
# If current element is smaller than or
# equal to pivot
# Instead of values, ASCII % m values
# are compared
if (a <= piv) :
# Increment index of smaller element
i += 1;
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1);
""" The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index """
def quickSort(arr, low, high, mod) :
if (low < high) :
''' pi is partitioning index,
arr[p] is now at right place '''
pi = partition(arr, low, high, mod);
# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi - 1, mod);
quickSort(arr, pi + 1, high, mod);
return arr
# Function to print the given array
def printArray(arr, size) :
for i in range(0, size) :
print(arr[i], end = " ");
# Driver code
if __name__ == "__main__" :
arr = [ 'g', 'e', 'e', 'k', 's' ];
n = len(arr);
mod = 8;
# Sort the given array
arr = quickSort(arr, 0, n - 1, mod);
# Print the sorted array
printArray(arr, n);
# This code is contributed by AnkitRai01
CPP
// C++ implementation of the approach
#include
using namespace std;
int M;
// Comparator used to sort the array
// according to ASCII % M
bool comparator(char ch1, char ch2)
{
int i = ch1 % M;
int j = ch2 % M;
return (i < j);
}
// Function to print the given array
void printArray(char arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
char arr[] = { 'g', 'e', 'e', 'k', 's' };
int n = sizeof(arr) / sizeof(arr[0]);
M = 8;
// Sort the given array
sort(arr, arr + n, comparator);
// Print the sorted array
printArray(arr, n);
return 0;
}
输出:
k s e e g
时间复杂度: O(n * log n)
辅助空间: O(1)
方法2:排序也可以使用std :: sort()并修改比较器来完成。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
int M;
// Comparator used to sort the array
// according to ASCII % M
bool comparator(char ch1, char ch2)
{
int i = ch1 % M;
int j = ch2 % M;
return (i < j);
}
// Function to print the given array
void printArray(char arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
char arr[] = { 'g', 'e', 'e', 'k', 's' };
int n = sizeof(arr) / sizeof(arr[0]);
M = 8;
// Sort the given array
sort(arr, arr + n, comparator);
// Print the sorted array
printArray(arr, n);
return 0;
}
输出:
k s e e g
时间复杂度: O(n * log n)
辅助空间: O(1)