<笔试><面试>编写一个排序函数,实现,既可以排序整形数组,又可以排序字符串。

思路:仿照qsort函数运用函数指针实现排序整形数组、排序字符串的功能。

首先,编写整形数组比较函数和字符串比较函数;
其次,仿照qsort函数将首元素地址、元素个数、元素大小、比较函数 传参编写熟悉的冒泡排序函数;
最后,举例验证函数正确性。

/******************************/
//1.编写冒泡排序,可以排序整形数组,也可以排序字符串。

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

void bobble(const void *base, int length, int width, int(*fcmp)(const void *a, const void *b))//(*fcmp)()函数指针
{
 assert(base);//断言
 int i = 0, j = 0, k = 0;
 char *arr = (char *)base;
 char temp=‘\0‘;
 for (i = 0; i < length - 1; i++)
  for (j = 0; j < length - i - 1; j++)
  {
   if ((*fcmp)(arr, arr + width * j)>0)//传参 将该元素地址与下一元素地址传入函数
   {
    for (k = 0; k < width * j; k++)//运用循环在内存中实现对两元素进行交换
    {
     temp = *(arr + k);
     *(arr + k) = *(arr + k + width * j);
     *(arr + k + width * j) = temp;
    }
   }
  }
}

int int_cmp1(const void *a, const void *b)
{
 assert(a);
 assert(b);
 return *(int *)a - *(int *)b;
}

int str_cmp(const void *a, const void *b)
{
 assert(a);
 assert(b);
 char *arr1 = (char *)a;
 char *arr2 = (char *)b;
 while (*arr1&&*arr2&&*arr1++ == *arr2++)
  ;
 return *(arr1 - 1) - *(arr2 - 1);
}

int main()
{
 int str1[] = { 1,5,0,2,4,7,9,6,3,5,6 ,16,155,26};
 int size = sizeof(str1) / sizeof(str1[0]);
 char str2[][30] = { "abcd","3456","cdef","1234" ,"abce"};
 int size_str = sizeof(str2) / sizeof(str2[0]);
 int i = 0;
 qsort(str1, size, sizeof(str1[0]), int_cmp1);//用快排函数结果作比较
 for (i = 0; i < size; i++)
  printf("%d ", str1[i]);
 printf("\n");
 bobble(str1, size, sizeof(str1[0]), int_cmp1);
 for (i = 0; i < size; i++)
  printf("%d ", str1[i]);
 printf("\n");

 qsort(str2, size_str, sizeof(str2[0]), str_cmp);//用快排函数结果作比较
 for (i = 0; i < size_str; i++)
  printf("%s\n", str2[i]);
 bobble(str2, size_str, sizeof(str2[0]), str_cmp);
 for (i = 0; i < size_str; i++)
  printf("%s\n", str2[i]);

 system("pause");
 return 0;
}

时间: 2024-08-27 22:55:37

<笔试><面试>编写一个排序函数,实现,既可以排序整形数组,又可以排序字符串。的相关文章

利用map和reduce编写一个str2float函数,把字符串&#39;123.456&#39;转换成浮点数123.456:

from functools import reduce CHAR_TO_FLOAT = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.': -1 } def str2float(s): nums=map(lambda x:CHAR_TO_FLOAT[x],s) #print(list(nums)) point = 0 def str_division(f,n): nonlo

随机数,字符数组与整形数组的捆绑排序

13.(***)模拟n个人参加选举的过程,并输出选举结果:假设候选人有四人,分别用A.B.C.D表示,当选某候选人时,直接输入其编号(编号由计算机随机产生), 若输入的不是A.B.C.D则视为无效票,选举结束后按得票数从高到低输出候选人编号和所得票数. 要求:取随机数,通过随机数结果判断选票结果并存储到数组中,然后通过下表同时将整形数组与对应的字符数组排序,输出结果. int main() { int people_num; printf("输入群众人数:"); scanf("

编写一个多线程函数实现对数组排序,要求: 1.至少用两个线程 2.数组的元素值可以事先定义好,或者可以从键盘输入(增加一个线程)。 3.用一个线程对数组排序,用另一个线程输出排序结果。 4.保证先排好序,再输出。

#include"stdio.h" #include"pthread.h" #include"semaphore.h" static int datbuf[10] = {0}; static int n; sem_t sem1,sem2,sem3; void *do_input(void *pvoid) { int i; sem_wait(&sem1); printf("please input data\n"); f

编写一个js函数,该函数有一个n(数字类型),其返回值是一个数组,该数组内是n个随机且不重复的整数,且整数取值范围是[2,32]

今天在公众号里边看到这个问题,就自己写了下,发现自己还是有许多没注意到的,在此记录, //返回一个随机整数 function getRandom( min , max ){ var rand = Math.round( Math.random() * (max - min) + min ); return rand; }; //重复性验证 function isRepeat(arr,n){ if (arr.indexOf(n) > -1) { return true; }; return fal

编写一个模版函数count

返回值是数组的a[0:n-1]的数组个数. 知识点:数组的两个特殊性质对我们定义和使用作用在数组上的函数有影响,这两个性质分别是:不允许拷贝数组以及使用数组时(通常)会将其转换成指针.因为不能拷贝数组,所以我们无法以值传递的方式使用数组参数.因为数组会被转换成指针,所以当我们为函数传递一个数组时,实际上传递的饰指向数组首元素的指针. ex: void print(const int*); void print(const int[]); void print(const int[10]) 答案:

MS笔试中的一个关于函数返回的“小”题

Which of following C++ code is correct ? A. int f() { int *a = new int(3); return *a; } B. int *f() { int a[3] = {1,2,3}; return a; } C. vector<int> f() { vector<int> v(3); return v; } D. void f(int *ret) { int a[3] = {1,2,3}; ret = a; return

编写一个javscript函数 fn,该函数有一个参数 n(数字类型),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。

首先定义个fn用来返回整数的取值范围: function getRand(a,b){ var rand = Math.ceil(Math.random()*(b-a)+a); return rand; } 再写一个func用来做去重校验: function checkArrIn(rand,array){ if(array.indexOf(rand) > -1){ return true } return false; } 最后是整体实现: function fn(n, min, max) { v

编写一个模块函数fill

给数组a[start:end-1]赋值value. 官方答案: template <class T> void fill(T* a, int start, int end, const T& value) {// Set a[start:end-1]. for (int i = start; i < end; i++) a[i] = value; }

编写一个memmove函数,实现内存拷贝

void* my_memmove(void *des,const void *src,int size) { char *Des=(char*)des; char *Src=(char*)src; assert(Des && Src); int len=strlen(Src); assert(size >0 && size <len); if(Des < Src || Des+size > Src+len) { while(size) { *Des+