导航:首页 > 源码编译 > sort函数的源码

sort函数的源码

发布时间:2022-06-07 11:35:04

Ⅰ 设计一个一维数组的排序函数p_sort,并调用它对10个整数进行排序。p_sort函数原型如下:

参考代码如下:

#include<stdio.h>
#defineN10
voidp_sort(int*p,intn)
{
inti,j,t;
if(p==NULL)
return;
for(i=0;i<n-1;++i)
for(j=0;j<n-i-1;++j)
if(p[j]>p[j+1]){
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
intmain()
{
inti,a[N];
for(i=0;i<N;++i)
scanf("%d",&a[i]);
p_sort(a,N);
printf("排序后: ");
for(i=0;i<N;++i)
printf("%d",a[i]);
return0;
}

Ⅱ 高分求C++实现插入排序源代码(可以运行)

下面的sort函数就是插入排序函数(直接插入排序),给你做了一个main函数进行验证

#include<iostream>
#include<iomanip>
using namespace std;
void sort(int a[],int n)//直接插入排序(从小到大)
{
int r;//r是“哨兵”
for(int i=1;i<n;i++)
if(a[i]<a[i-1])
{
r=a[i];
a[i]=a[i-1];
for(int j=i-1;r<a[j];j--)a[j+1]=a[j];
a[j+1]=r;
}
}
void main()
{
int a[10]={1,2,16,45,23,99,18,67,42,10};
sort(a,10);
for(int i=0;i<10;i++)cout<<setw(4)<<a[i];
cout<<endl;
}

Ⅲ 跪求C++sort函数源代码怎么写的

你可以直接看源码的。。。。。,API上面什么都有,学语言都要看API的

Ⅳ c++里sort函数里的比较函数怎么写

sort接受的是参数是指针或迭代器,sort(a[0],a[n]);你这里只是2个元素。

可以自写比较函数,也可以用标准定义好的函数对象:

#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

bool sort_desc(int a, int b)
{
return a > b;
}
bool sort_asc(int a, int b)
{
return a < b;
}

void p(int* begin, int* end)
{
while(begin < end)
cout << *begin++ << ' ';
cout << endl;
}

int main()
{
int a[] = {6,9,1,3,5,2,7,0,4,8};

sort(a, a + 10, sort_desc);
p(a, a + 10);
sort(a, a + 10, sort_asc);
p(a, a + 10);

sort(a, a + 10, greater<int>());
p(a, a + 10);
sort(a, a + 10, less<int>());
p(a, a + 10);
}

Ⅳ js中sort方法的源码,该怎么解决

unction ArraySort(comparefn) {
// In-place QuickSort algorithm.
// For short (length <= 22) arrays, insertion sort is used for efficiency.

var custom_compare = IS_FUNCTION(comparefn);

function Compare(x,y) {
// Assume the comparefn, if any, is a consistent comparison function.
// If it isn't, we are allowed arbitrary behavior by ECMA 15.4.4.11.
if (x === y) return 0;
if (custom_compare) {
// Don't call directly to avoid exposing the builtin's global object.
return comparefn.call(null, x, y);
}
if (%_IsSmi(x) && %_IsSmi(y)) {
return %SmiLexicographicCompare(x, y);
}
x = ToString(x);
y = ToString(y);
if (x == y) return 0;
else return x < y ? -1 : 1;
};

function InsertionSort(a, from, to) {
for (var i = from + 1; i < to; i++) {
var element = a[i];
// Pre-convert the element to a string for comparison if we know
// it will happen on each compare anyway.
var key =
(custom_compare || %_IsSmi(element)) ? element : ToString(element);
// place element in a[from..i[
// binary search
var min = from;
var max = i;
// The search interval is a[min..max[
while (min < max) {
var mid = min + ((max - min) >> 1);
var order = Compare(a[mid], key);
if (order == 0) {
min = max = mid;
break;
}
if (order < 0) {
min = mid + 1;
} else {
max = mid;
}
}
// place element at position min==max.
for (var j = i; j > min; j--) {
a[j] = a[j - 1];
}
a[min] = element;
}
}

function QuickSort(a, from, to) {
// Insertion sort is faster for short arrays.
if (to - from <= 22) {
InsertionSort(a, from, to);
return;
}
var pivot_index = $floor($random() * (to - from)) + from;
var pivot = a[pivot_index];
// Pre-convert the element to a string for comparison if we know
// it will happen on each compare anyway.
var pivot_key =
(custom_compare || %_IsSmi(pivot)) ? pivot : ToString(pivot);
// Issue 95: Keep the pivot element out of the comparisons to avoid
// infinite recursion if comparefn(pivot, pivot) != 0.
a[pivot_index] = a[from];
a[from] = pivot;
var low_end = from; // Upper bound of the elements lower than pivot.
var high_start = to; // Lower bound of the elements greater than pivot.
// From low_end to i are elements equal to pivot.
// From i to high_start are elements that haven't been compared yet.
for (var i = from + 1; i < high_start; ) {
var element = a[i];
var order = Compare(element, pivot_key);
if (order < 0) {
a[i] = a[low_end];
a[low_end] = element;
i++;
low_end++;
} else if (order > 0) {
high_start--;
a[i] = a[high_start];
a[high_start] = element;
} else { // order == 0
i++;
}
}
QuickSort(a, from, low_end);
QuickSort(a, high_start, to);
}

var old_length = ToUint32(this.length);
if (old_length < 2) return this;

%RemoveArrayHoles(this);

var length = ToUint32(this.length);

// Move undefined elements to the end of the array.
for (var i = 0; i < length; ) {
if (IS_UNDEFINED(this[i])) {
length--;
this[i] = this[length];
this[length] = void 0;
} else {
i++;
}
}

QuickSort(this, 0, length);

// We only changed the length of the this object (in
// RemoveArrayHoles) if it was an array. We are not allowed to set
// the length of the this object if it is not an array because this
// might introce a new length property.
if (IS_ARRAY(this)) {
this.length = old_length;
}

return this;
}

Ⅵ 求C#里sort()函数封装的源代码

同意楼上说法,用reflector就可以,这是其中的一部分代码,建议去看reflector。

1.Sort() : Void
public void Sort()
{
this.Sort(0, this.Count, null);
}
2.Sort(IComparer<T>):Void
public void Sort(IComparer<T> comparer)
{
this.Sort(0, this.Count, comparer);
}
1和2的this.Sort:
public void Sort(int index, int count, IComparer<T> comparer)
{
if ((index < 0) || (count < 0))
{
ThrowHelper.((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
if ((this._size - index) < count)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen);
}
Array.Sort<T>(this._items, index, count, comparer);
this._version++;
}

3.Sort(Comparison<T>):Void
public void Sort(Comparison<T> comparison)
{
if (comparison == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
}
if (this._size > 0)
{
IComparer<T> comparer = new Array.FunctorComparer<T>(comparison);
Array.Sort<T>(this._items, 0, this._size, comparer);
}
}
最后一行的Sort:
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
public static void Sort<T>(T[] array, int index, int length, IComparer<T> comparer)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if ((index < 0) || (length < 0))
{
throw new ArgumentOutOfRangeException((length < 0) ? "length" : "index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((array.Length - index) < length)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
if ((length > 1) && (((comparer != null) && (comparer != Comparer<T>.Default)) || !TrySZSort(array, null, index, (index + length) - 1)))
{
ArraySortHelper<T>.Default.Sort(array, index, length, comparer);
}
}

Ⅶ 在c语言中运用sort函数的代码示例,最好简单,能够包含基础的知识点和基本格式

好吧,来个冒泡法排序的例子;假设有一个整形数组 a[100];数据已输入;现在对其进行升序。

for(inti=0;i<99;i++)//依次取数组0-98个数
{
for(intj=i+1;j<100;j++)//取出的数和后面的每个数进行比较
{
if(a[i]>a[j])//如果比后面的数大
{
inttmp=a[i];//两个数交换,
a[i]=b[i];
b[i]=tmp;
}//一遍循环把最小a[i]到a[99]中的最小的数“冒”到a[i]
}
}

Ⅷ sort()排序函数,特想知道下面的排序是怎么完成的

sort是js数组类型内建的native函数。也就是内置的函数。
alert(Array.prototype.sort);
会提示function sort() { [native code] }
由于是二进制的本地代码,无法查看源码。

Ⅸ C++中sort() 是如何实现的

// 插入排序实现:
void SortInsert::insertSort(vector<int>::iterator begin, vector<int>::iterator end)
{
for (vector<int>::iterator i = begin + 1; i < end; ++i)
{
for(vector<int>::iterator j = i; *j < *(j - 1); --j )
{
std::iter_swap((j - 1), j);
}
}
}

// 冒泡排序实现:
#include <algorithm>

template<typename Iterator>
void bubbleSort(Iterator first, Iterator last)
{
Iterator i, j;
for (i = first; i != last; i++)
for (j = first; j < i; j++)
if (*i < *j)
std::iter_swap(i, j); // or std::swap(*i, *j);
}

template<typename Iterator, class StrictWeakOrdering>
void bubbleSort(Iterator first, Iterator last, StrictWeakOrdering compare)
{
Iterator i, j;
for (i = first; i != last; i++)
for (j = first; j < i; j++)
if (compare(*i, *j))
std::iter_swap(i, j);
}

// 选择
#include <algorithm> // for: std::iter_swap, std::min_element

template <typename Iterator>
void selection_sort(Iterator begin, Iterator end)
{
Iterator min;
while (begin != end)
{
min = std::min_element(begin, end);
std::iter_swap(begin, min);
++begin;
}
}

// 快速
#include <functional>
#include <algorithm>
#include <iterator>

template< typename BidirectionalIterator, typename Compare >
void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) {
if( first != last ) {
BidirectionalIterator left = first;
BidirectionalIterator right = last;
BidirectionalIterator pivot = left++;

while( left != right ) {
if( cmp( *left, *pivot ) ) {
++left;
} else {
while( (left != right) && cmp( *pivot, *right ) )
--right;
std::iter_swap( left, right );
}
}

--left;
std::iter_swap( pivot, left );

quick_sort( first, left, cmp );
quick_sort( right, last, cmp );
}
}

template< typename BidirectionalIterator >
inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) {
quick_sort( first, last,
std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >()
);
}

===========算啦,你还是去这里(下面是链接)看看吧,共同学习哦!!
http://en.wikibooks.org/w/index.php?title=Special%3ASearch&redirs=1&search=sort&fulltext=Search&ns0=1&ns4=1&ns112=1

Ⅹ C程序 sort函数

sort函数中
float savior[10][7];
int i,j;
for(i=0;i<10;i++)
{
savior[i][0]=stu[i][0];
savior[i][1]=stu[i][1];
for(j=2;j<8;j++)
savior[i][j]=score[i][j-2];
}
j的最大值是7,但savior数组标号最大为savior[9][6],for循环改为for(j=2;j<7;j++)

下面这个if(stu[i][1]<stu[i+1][1])
我没看到你对stu[x][1]这一列的任何数组元素赋值,那么这个语句有什么用呢?

float *temp=&savior[0][0];这个语句对之赋值毫无意义啊,后面的程序根本用不到

阅读全文

与sort函数的源码相关的资料

热点内容
積架小型空气压缩机 浏览:553
绿盾文档加密系统哪里有卖 浏览:635
我的世界怎么开挂在服务器里面 浏览:787
西门子自锁正反转编程图 浏览:747
出国英语pdf 浏览:918
算法线性匹配 浏览:671
山东省dns服务器云主机 浏览:552
安卓5g软件怎么隐藏 浏览:837
编译内核空间不足开不了机 浏览:884
汉纪pdf 浏览:471
在哪里下载国家医保app 浏览:654
没有与文件扩展关联的编译工具 浏览:425
我的世界反编译mcp下载 浏览:18
安卓手柄下载什么软件 浏览:67
pushrelabel算法 浏览:848
硬盘资料部分文件夹空白 浏览:614
cssloader的编译方式 浏览:937
java面板大小 浏览:501
怎么用命令方块打出字体 浏览:497
台湾加密货币研究小组 浏览:294