`
wss71104307
  • 浏览: 223028 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

quick sort program

J# 
阅读更多
#include <stdio.h>
#include <stdlib.h>


void swap(int & a,int & b)
{
	int temp=a;
	a=b;
	b=temp;
}
int QuickSort_Partition(int A[],int p, int r)
{
	//随机选值

	int s_max=rand()%(r-p+1)+p;//随机数生产后要加上p

	swap(A[s_max],A[r]);//随机数生成后注意交换

	int max=A[r];

	int i=p-1;
	int j=p;

	while(j<r)
	{
		if(A[j]<=max)
		{
			swap(A[j],A[++i]);
		}
		j++;
	}
    
	swap(A[r],A[++i]);//注意交换

    return i;
}

void QuickSort(int A[],int p, int r)
{
	if(p>=r) return;
	int q=QuickSort_Partition(A,p,r);

	QuickSort(A,p,q-1);
	QuickSort(A,q+1,r);
}



int main()
{
	int A[]={5,4,7,6,3,8,11,98,44,33};
	QuickSort(A,0,9);
	printf("\n");
	for(int i=0;i<=9;i++)
	printf("%d\t",A[i]);
	printf("\n");
}

 需要几个地方注意,已经写在上面了,

分享到:
评论

相关推荐

    quick sort

    Use QuickSort algorithm to sort the array that has n elements that are constructed by the random() function. Requirements: The template should be used for all kinds of data type, such as: integer, ...

    Data.Structures.and.Algorithms.USING.C

    26. Quick Sort GRAPH DATA STRUCTURE 27. Graphs 28. Depth First Traversal 29. Breadth First Traversal TREE DATA STRUCTURE 30. Tree 31. Tree Traversal 32. Binary Search Tree 33. AVL Trees 34. Spanning...

    十大排序算法的C#代码

    4. **快速排序**(Quick Sort):由C.A.R. Hoare提出的,通过选取一个基准值,将数组分为两部分,一部分所有元素小于基准,另一部分所有元素大于基准,然后对这两部分递归进行快速排序。 5. **希尔排序**(Shell ...

    WebAssembly: Accessing C and C++ in Web Applications

    Programmer and engineer Matt Scarpino demonstrates the power of the WebAssembly, implementing quick sort and matrix multiplication algorithms. He also reviews advanced features, such as debugging and...

    The Little SAS Book(Fourth)

    This clear and concise format enables new users to get up and running quickly, while the examples allow readers to type in the program and see it work! New topics in the fourth edition include: ODS...

    Estrutura-de-Dados:Estrutura de Dados-Estudos

    8. **Algoritmos de ordenação**: Algoritmos como Bubbl sort, Selection sort, Insertion sort, Quick sort e Merge sort são estudados e implementados usando Pascal, pois eles exigem manipulação de ...

    快速排序C#程序

    Console.WriteLine(" Quick Sort Test!!!"); for (int i = 0; i ; i++) { Console.WriteLine(arr[i]); } Console.ReadKey(); } static int partion(int[] a, int low, int high) { int i = low; int j =...

    C语言程序设计源程序:通讯录.pdf

    Quick seek\n"); printf("10. Copy file\n"); printf("11. Exit program\n\n"); printf("Enter your choice: "); gets(s); c = atoi(s); return c; } /******函数定义******/ /* 输入记录 */ int enter(ADDRESS t[]...

    VB编程资源大全(英文源码 控制)

    start your Visual Basic program with your project code showing, right click and you should see "Rem Builder".&lt;END&gt;&lt;br&gt;3 , syntax.zip This is an excellent example of how to highlight ...

    python programming

    1.2 Program Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 What is Computer Science? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    Cody‘s_Data_Cleaning_Techniques_Using_SAS_(Second_Edtion)

    - **Eliminating Duplicates by Using PROC SORT**: Explains how to eliminate duplicate records using PROC SORT. - **Detecting Duplicates by Using DATA Step Approaches**: Describes various DATA step ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    Each exercise is structured to be completed within approximately ten minutes, making it ideal for quick learning sessions. The exercises cover various aspects of the Python programming language, ...

    C++ Programming HOW TO

    STL 包含了许多容器类,如vector、list和map,以及一系列通用算法,如sort和find。 #### 24. Threads in C++ 多线程编程是C++中的一个高级主题,它涉及到并发执行多个任务。 #### 25. C++ Utilities 实用工具...

    GENESIS 2000 DFM PE

    The DFM Programming Environment is one of the many unique concepts of the Genesis 2000 system. Unlike any existing tool, the DFM Programming Environment (DFMPE) enables any user with elementary ...

    Digging into WordPress.pdf

    1.4.5 FTP Program .............................................................................19 2 Setting Up WordPress 2.1.1 The Famous Five Minute Install .............................23 2.1.2 ...

Global site tag (gtag.js) - Google Analytics