优化以下代码 void sort(char *p[N]) { char *t; int i = 0,j = 0; while (i < N - 1) { while (j < N - 1) { if (strcmp(p[j + 1],p[j]) < 0) { t = p[j + 1]; p[j + 1] = p[j]; p[j] = t; } j++; } j = 0; i++; } }
时间: 2023-11-28 19:50:02 浏览: 154
这个排序算法是冒泡排序,可以进行以下优化:
1. 添加一个标志位,表示本轮是否进行了交换。如果没有进行交换,则说明数组已经有序,可以直接跳出循环。
2. 内层循环不需要遍历整个数组,因为每一轮都会将当前未排序的最大值放到数组最后,所以下一轮内层循环只需要遍历到 N-i-1 即可。
下面是优化后的代码:
```
void sort(char *p[N]) {
char *t;
int i = 0, j = 0, flag = 1;
while (i < N - 1 && flag) {
flag = 0;
for (j = 0; j < N - i - 1; j++) {
if (strcmp(p[j + 1], p[j]) < 0) {
t = p[j + 1];
p[j + 1] = p[j];
p[j] = t;
flag = 1;
}
}
i++;
}
}
```
相关问题
完善代码1.冒泡排序 2.快速排序 3.堆排序#include <iostream> #include<cstdlib> #include<Windows.h> #include<ctime> using namespace std; void randomAssign(int a[],int n){ srand(time(0)); for(int i = 0; i < n;i++) a[i] = rand()%n+1; } void print(const char* str,int a[],int n){ cout<<str<<"="; for(int i = 0; i < n; i++) cout<<a[i] <<" "; cout<<endl; } void bubbleSort(int a[],int n){ for(int i = n; i >= 1; i--) for(int j = 1; j < i; j++) if(a[j+1] > a[j]) swap(a[j+1],a[j]); } int onePartition(int a[],int left,int right){ int pivot = a[left]; int i = left; int j = right; while(i < j){ while(XXXXXXXXXXXXX;) j++; if( i < j) a[i] = a[j]; XXXXXXXXXXXXX; XXXXXXXXXXXXX; if( i < j) a[j] = a[i]; } XXXXXXXXXXXXX; return i; } void quickSort(int a[],int left,int right){ if(left >= right) return ; int pos = XXXXXXXXXXXXX;; XXXXXXXXXXXXX; quickSort(a,pos+1,right); } void quickSort(int a[],int n){ quickSort(a,0,n-1); } void sift(int a[],int pos,int n){ //n表示长度 int parent = pos; int son = parent * 2 + 1; int tmp = a[parent]; while (son <= n - 1){ // 有效下标 0...n-1 if (son + 1 <= n - 1 && a[son] < a[son + 1]) son++; if(XXXXXXXXXXXXX) return; else { XXXXXXXXXXXXX; parent = son; son = parent * 2 + 1; } } } void heapSort(int a[],int n) { int i = 0; for (i = n / 2 - 1; i >= 0; i--) //从最后一个非叶结点开始 sift(a,i,n); for (i = n-1; i> 0; i--) { swap(a[0],a[i]); XXXXXXXXXXXXX; } } typedef void(*SortFunc)(int[], int); void testSort(SortFunc sortFunc,int n = 10){ DWORD start, end; int* a = new int[n]; randomAssign(a,n); if(n < 20) print("排序前",a,n); start = GetTickCount(); sortFunc(a,n); end = GetTickCount(); if(n < 20) print("排序后",a,n); cout<<"花费时间="<<end - start<<"毫秒"<<endl<<endl; delete [] a; } int main(){ testSort(bubbleSort,10); testSort(quickSort,10); testSort(heapSort,10); return 1; }
冒泡排序、快速排序和堆排序的代码已经提供,下面是完整的代码:
```c++
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include <ctime>
using namespace std;
void randomAssign(int a[], int n) {
srand(time(0));
for (int i = 0; i < n; i++)
a[i] = rand() % n + 1;
}
void print(const char* str, int a[], int n) {
cout << str << "=";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
void bubbleSort(int a[], int n) {
for (int i = n; i >= 1; i--)
for (int j = 1; j < i; j++)
if (a[j + 1] > a[j])
swap(a[j + 1], a[j]);
}
int onePartition(int a[], int left, int right) {
int pivot = a[left];
int i = left;
int j = right;
while (i < j) {
while (a[j] >= pivot && i < j)
j--;
if (i < j)
a[i++] = a[j];
while (a[i] <= pivot && i < j)
i++;
if (i < j)
a[j--] = a[i];
}
a[i] = pivot;
return i;
}
void quickSort(int a[], int left, int right) {
if (left >= right)
return;
int pos = onePartition(a, left, right);
quickSort(a, left, pos - 1);
quickSort(a, pos + 1, right);
}
void quickSort(int a[], int n) {
quickSort(a, 0, n - 1);
}
void sift(int a[], int pos, int n) { // n表示长度
int parent = pos;
int son = parent * 2 + 1;
int tmp = a[parent];
while (son <= n - 1) { // 有效下标 0...n-1
if (son + 1 <= n - 1 && a[son] < a[son + 1])
son++;
if (tmp >= a[son])
break;
else {
a[parent] = a[son];
parent = son;
son = parent * 2 + 1;
}
}
a[parent] = tmp;
}
void heapSort(int a[], int n) {
int i = 0;
for (i = n / 2 - 1; i >= 0; i--) //从最后一个非叶结点开始
sift(a, i, n);
for (i = n - 1; i > 0; i--) {
swap(a[0], a[i]);
sift(a, 0, i);
}
}
typedef void(*SortFunc)(int[], int);
void testSort(SortFunc sortFunc, int n = 10) {
DWORD start, end;
int* a = new int[n];
randomAssign(a, n);
if (n < 20)
print("排序前", a, n);
start = GetTickCount();
sortFunc(a, n);
end = GetTickCount();
if (n < 20)
print("排序后", a, n);
cout << "花费时间=" << end - start << "毫秒" << endl << endl;
delete[] a;
}
int main() {
testSort(bubbleSort, 10);
testSort(quickSort, 10);
testSort(heapSort, 10);
return 1;
}
```
需要注意的是:
1. 在快速排序的 onePartition 函数中,i 和 j 的初始值应该分别为 left 和 right,而不是 left + 1 和 right - 1。这是因为在 while 循环中,i 和 j 分别向中间移动,不会越界。
2. 在堆排序的 sift 函数中,比较的应该是 tmp 和 a[son],而不是 a[parent] 和 a[son]。
3. 在堆排序的循环中,交换 a[0] 和 a[i] 后,应该对 a[0] 进行 sift 调整,而不是对 a[i] 进行 sift 调整。因为 a[i] 已经排好序了,不需要再调整。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #include<openssl/hmac.h> char *signature_calculate(char *json_obj, char *key){ unsigned char *key_byte = (unsigned char *)key; char *sorted_json = to_url_query(json_obj); unsigned char *dataddd = (unsigned char *)sorted_json; unsigned char *signature = HMAC(EVP_sha256(), key_byte, strlen(key), dataddd, strlen(dataddd), NULL, NULL); char hex_signature = malloc(2 * EVP_MAX_MD_SIZE + 1); for(int i=0; i<EVP_MAX_MD_SIZE; i++) { sprintf(&hex_signature[i2], "%02x", signature[i]); } return hex_signature; } typedef struct { char key[256]; char value[256]; } KeyValue; int compare(const void a, const void b) { return strcmp(((KeyValue)a)->key, ((KeyValue)b)->key); } char *sort_dict(KeyValue *array, int size) { // 对KeyValue数组按ASCII码升序排序 qsort(array, size, sizeof(KeyValue), compare); char *query_list = malloc(size * 256); int len=0; for(int i=0; i<size; i++) { if(strlen(array[i].value)==0){ // 如果值为空或者空字符串则不拼接 continue; } char *key = array[i].key; char *value = array[i].value; if(isalpha(value[0]) && isalnum(value[1]) && strcmp(value, "true")!=0 && strcmp(value, "false")!=0) { sprintf(&query_list[len], "%s=%s&", key, value); } else { sprintf(&query_list[len], "%s="%s"&", key, value); } len = strlen(query_list); } if(len>0) { query_list[len-1] = 0; } return query_list; } char *to_url_query(char *json, char *prefix){ // 将json字符串转换为URL键值对形式的字符串 int len = strlen(json); KeyValue *array = malloc(len * sizeof(KeyValue)); int i=0; int j=0; int level=0; char *key; // 处理嵌套字典的键名 while(i<len){ if(json[i]=='{' || json[i]=='['){ level++; i++; } else if(json[i]=='}' || json[i]==']'){ level--; i++; } else if(json[i]==','){ array[j].value[i-array[j].key] = 0; i++; j++; } else if(json[i]==':'){ key = array[j].key; array[j].value[0] = 0; i++; } else if(json[i]=='"' && level%2==0){ i++; int k=0; while(json[i]!='"') { array[j].value[k] = json[i]; i++; k++; } array[j].value[k] = 0; i++; } else if(json[i]!=',' && json[i]!=':' && json[i]!=' '){ array[j].key[i-j] = json[i]; i++; } else { i++; } } array[j].value[i-array[j].key] = 0; j++; char *sorted_json = sort_dict(array, j); char *query_list = malloc(strlen(sorted_json)+1); if(strlen(prefix)>0){ sprintf(query_list, "%s.%s", prefix, sorted_json); } else { strcpy(query_list, sorted_json); } free(array); free(sorted_json); return query_list; } 请对上面的代码添加注释
```
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<openssl/hmac.h>
// 计算签名函数,接收一个JSON字符串和一个密钥字符串,返回一个签名字符串
char *signature_calculate(char *json_obj, char *key){
// 将密钥转换为unsigned char类型的指针
unsigned char *key_byte = (unsigned char *)key;
// 将JSON字符串转换为URL键值对形式的字符串
char *sorted_json = to_url_query(json_obj);
// 将URL键值对形式的字符串转换为unsigned char类型的指针
unsigned char *dataddd = (unsigned char *)sorted_json;
// 使用OpenSSL库的HMAC函数计算签名
unsigned char *signature = HMAC(EVP_sha256(), key_byte, strlen(key), dataddd, strlen(dataddd), NULL, NULL);
// 将签名字符串转换为十六进制形式的字符串并返回
char hex_signature = malloc(2 * EVP_MAX_MD_SIZE + 1);
for(int i=0; i<EVP_MAX_MD_SIZE; i++) {
sprintf(&hex_signature[i2], "%02x", signature[i]);
}
return hex_signature;
}
// 定义一个键值对结构体类型,用于存储键值对
typedef struct {
char key[256];
char value[256];
} KeyValue;
// 对KeyValue数组按ASCII码升序排序的比较函数
int compare(const void a, const void b) {
return strcmp(((KeyValue)a)->key, ((KeyValue)b)->key);
}
// 将KeyValue数组按ASCII码升序排序并拼接成URL键值对形式的字符串
char *sort_dict(KeyValue *array, int size) {
// 对KeyValue数组按ASCII码升序排序
qsort(array, size, sizeof(KeyValue), compare);
// 初始化一个字符串,用于存储拼接后的URL键值对形式的字符串
char *query_list = malloc(size * 256);
int len=0;
for(int i=0; i<size; i++) {
// 如果值为空或者空字符串则不拼接
if(strlen(array[i].value)==0){
continue;
}
char *key = array[i].key;
char *value = array[i].value;
// 如果值是字母或数字,则直接拼接
if(isalpha(value[0]) && isalnum(value[1]) && strcmp(value, "true")!=0 && strcmp(value, "false")!=0) {
sprintf(&query_list[len], "%s=%s&", key, value);
} else { // 否则需要将值加上双引号再拼接
sprintf(&query_list[len], "%s="%s"&", key, value);
}
len = strlen(query_list);
}
// 去掉最后一个&符号
if(len>0) {
query_list[len-1] = 0;
}
return query_list;
}
// 将JSON字符串转换为URL键值对形式的字符串
char *to_url_query(char *json, char *prefix){
// 计算JSON字符串的长度
int len = strlen(json);
// 初始化一个KeyValue数组,用于存储键值对
KeyValue *array = malloc(len * sizeof(KeyValue));
int i=0;
int j=0;
int level=0;
char *key;
// 处理嵌套字典的键名
while(i<len){
if(json[i]=='{' || json[i]=='['){ // 如果遇到{或[,则进入下一层级
level++;
i++;
} else if(json[i]=='}' || json[i]==']'){ // 如果遇到}或],则退出上一层级
level--;
i++;
} else if(json[i]==','){ // 如果遇到逗号,则处理下一个键值对
array[j].value[i-array[j].key] = 0;
i++;
j++;
} else if(json[i]==':'){ // 如果遇到冒号,则表示下一个字符串是值,保存键名
key = array[j].key;
array[j].value[0] = 0;
i++;
} else if(json[i]=='"' && level%2==0){ // 如果遇到双引号,则表示下一个字符串是键名或值
i++;
int k=0;
while(json[i]!='"') { // 保存双引号之间的字符串
array[j].value[k] = json[i];
i++;
k++;
}
array[j].value[k] = 0;
i++;
} else if(json[i]!=',' && json[i]!=':' && json[i]!=' '){ // 如果不是逗号、冒号或空格,则表示当前字符是键名的一部分
array[j].key[i-j] = json[i];
i++;
} else { // 否则忽略当前字符
i++;
}
}
// 保存最后一个键值对的值
array[j].value[i-array[j].key] = 0;
j++;
// 将KeyValue数组按ASCII码升序排序并拼接成URL键值对形式的字符串
char *sorted_json = sort_dict(array, j);
// 初始化一个字符串,用于存储拼接后的URL键值对形式的字符串
char *query_list = malloc(strlen(sorted_json)+1);
if(strlen(prefix)>0){ // 如果有前缀,则在拼接后的字符串前加上前缀
sprintf(query_list, "%s.%s", prefix, sorted_json);
} else { // 否则直接返回拼接后的字符串
strcpy(query_list, sorted_json);
}
// 释放内存
free(array);
free(sorted_json);
return query_list;
}
```
阅读全文
相关推荐

















