合并排序算法排序过程_合并排序| 用于大型输入的最佳排序算法之一

合并排序算法排序过程

What is sorting?

什么是分类?

Sorting allows us to process our data in a more organized and efficient way. It makes searching easy as it will now take less time to search for a specific value in a given sorted sequence with respect to a sequence which was initially unsorted.

排序使我们能够以更有条理和有效的方式处理数据。 它使搜索变得容易,因为相对于最初未排序的序列,现在将花费更少的时间在给定的排序序列中搜索特定值。

In programming, there are any numbers of sorting algorithms, some of them are given below,

在编程中,有许多排序算法,下面给出其中一些,

  1. Bubble sort

    气泡排序

  2. Selection sort

    选择排序

  3. Insertion sort

    插入排序

  4. Merge sort

    合并排序

  5. Quick sort

    快速分类

  6. Randomized Quick sort (an optimized quick sort)

    随机快速分类(一种优化的快速分类)

But, the problem with such sorting algorithms like bubble sort, insertion sort, and the selection sort is they take a lot of time to sort.

但是,诸如气泡排序,插入排序和选择排序之类的排序算法的问题在于它们需要大量时间来排序。

For example, If we have to sort an array of 10 elements then any sorting algorithm can be opted but in case of an extensively high value of N that is the no. of elements of the array like if N=1000000 then in case the starting 3 sorting algorithms cannot be opted as the time they will take is proportional to (N*N) which in big O notation can be represented as O(N*N).

例如,如果我们必须对10个元素的数组进行排序,则可以选择任何排序算法,但是如果N的值很高,则为否。 数组的元素,例如,如果N = 1000000,则万一开始的3种排序算法无法选择,因为它们花费的时间与(N * N)成正比,则在大O表示中可以表示为O(N * N)

So today we will focus on a more optimized sorting algorithm that is (MERGE SORT).

因此,今天我们将重点放在一种更优化的排序算法( MERGE SORT )上。

Basically, both merge and quick sort are divide and conquer algorithms.

基本上,合并和快速排序都是分治算法

But today we'll be focusing on MERGE SORT and the main reason of casting light upon this sorting algorithm is it takes O(N*logN) time which is very efficient with respect to the O(N*N).

但是今天,我们将重点讨论MERGE SORT,并且将光线投在此排序算法上的主要原因是,它需要O(N * logN)时间,这相对于O(N * N)非常有效。

In merge sort we follow just 3 simple steps to sort an array:

在合并排序中,我们仅需3个简单步骤即可对数组进行排序:

  1. Divide the array into two parts

    将数组分为两部分

  2. Recursively sort both the parts

    对两个部分进行递归排序

  3. Then, merge those two stored parts into one

    然后,将这两个存储的部分合并为一个

Description:

描述:

So basically it is considered as one of the best sorting algorithms having a worst case and best case time complexity of O(N*Log(N)), this is the reason that generally we prefer to merge sort over quicksort as quick sort does have a worst-case time complexity of O(N*N).

因此,基本上,它被认为是O(N * Log(N))的最坏情况和最坏情况下时间复杂度最好的排序算法之一,这就是为什么我们通常更喜欢像快速排序那样合并排序而不是快速排序O(N * N)的最坏情况下的时间复杂度。

Let’s quickly jump upto the coding part...

让我们快速跳到编码部分...

#include<iostream>
using namespace std;
int temp[10000];
void mergearrays(int ar[],int s,int e)
{
int mid=(s+e)/2;
int i,j;
i=s;  j=mid+1;
int x=s;
while(i<=mid&&j<=e)
{
if(ar[i]<ar[j])
{
temp[x++]=ar[i];
i++;
}
else
{
temp[x++]=ar[j];
j++;
}
}
while(i<=mid)
{
temp[x++]=ar[i];
i++;
}
while(j<=e)
{
temp[x++]=ar[j];
j++;
}
for(int i=s;i<=e;i++)
ar[i]=temp[i];
}
void mergesort(int ar[],int s,int e)
{
int mid=(s+e)/2;
///base case
if(s>=e)
return ;
///recursive case
mergesort(ar,s,mid);
mergesort(ar,mid+1,e);
mergearrays(ar,s,e);
}
int main()
{
int n;
cout<<"Enter total number of elements: ";	
cin>>n;
int ar[10000];
cout<<"The unsorted array is (Enter elements): "<<endl;
for(int i=0;i<n;i++)
cin>>ar[i];
mergesort(ar,0,n-1);
cout<<"The sorted array is"<<endl;
for(int i=0;i<n;i++)
cout<<ar[i]<<" ";
return 0;
}

Output

输出量

Enter total number of elements: 7
The unsorted array is (Enter elements):
7 4 6 5 3 2 1
The sorted array is
1 2 3 4 5 6 7

翻译自: https://www.includehelp.com/algorithms/merge-sort-one-of-the-best-sorting-algorithms-used-for-large-inputs.aspx

合并排序算法排序过程

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380611.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux:jumpserver介绍(1)

官方网站 JumpServer - 开源堡垒机 - 官网https://www.jumpserver.org/ JumpServer 是广受欢迎的开源堡垒机&#xff0c;是符合 4A 规范的专业运维安全审计系统。JumpServer 帮助企业以更安全的方式管控和登录所有类型的资产&#xff0c;实现事前授权、事中监察、事后审计&…

对一个简单汇编程序分析

程序&#xff1a; assume cs:codesgcodesg segmentmov ax,0123Hmov bx,0456Hadd ax,bxadd ax,axmov ax,4c00Hint 21Hcodesg endsend伪指令&#xff1a; 伪指令是写给编译器看的&#xff0c;CPU不会执行&#xff0c;在源程序中&#xff0c;包括两种指令&#xff0c;一个是…

datagrid 页眉合并

http://www.codeproject.com/aspnet/MergeDatagridHeader.asp private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e){//产生序号&#xff0c;合并单元格if(e.Item.ItemIndex!-1){e.Item.Cells[1].TextConvert.ToString(e.Item.ItemIndex1);//}else{e…

劈尖等厚干涉条纹matlab,劈尖等厚干涉实验中,k=0级的干涉条纹是条纹,与k级暗条纹对应的空气薄膜的厚度为...

劈尖等厚干涉实验中&#xff0c;k0级的干涉条纹是条纹&#xff0c;与k级暗条纹对应的空气薄膜的厚度为答&#xff1a;暗&#xff0c;kλ/2spampython 编程\nprint(spam[-6:-4])是否报错&#xff1f;(是&#xff1a;则填写报错原因&#xff0c;否&#xff1a;则填写输出结果)答&…

使用OpenCV python模块读取图像并将其另存为灰度系统

In Python, we can use an OpenCV library named cv2. Python does not come with cv2, so we need to install it separately. 在Python中&#xff0c;我们可以使用名为cv2的OpenCV库 。 Python没有随附cv2 &#xff0c;因此我们需要单独安装它。 For Windows: 对于Windows&a…

loop指令

功能&#xff1a;循环 格式&#xff1a;loop 标号 执行过程&#xff1a; cxcx-1判断cx的值&#xff0c;不为0则转至标号处执行程序&#xff0c;如果为0则向下执行 从上面的执行过程可以看出&#xff0c;cx存放的是循环次数 举个例子&#xff1a;实现2的12次方 assume cs:co…

不知不觉中学会做一个有主见的人

今天还是经历了不少事情&#xff0c;首先中午定好了聚会吃饭的地方&#xff0c;通知了以前班里的同学。事情解决了觉得心里很开心。中午不睡觉去人文馆准备听微软的宣讲会。等到三点多稀稀疏疏的几个人在那里&#xff0c;一个保安跑进来说取消了。埃&#xff0c;居然不通知。虽…

C# datetime 操作

C# datetime 操作 //C#里内置的DateTime基本上都可以实现这些功能&#xff0c;巧用DateTime会使你处理这些事来变轻松多了 //今天 DateTime.Now.Date.ToShortDateString(); //昨天&#xff0c;就是今天的日期减一…

php 子网掩码正则,验证子网掩码正则表达式代码范例

验证子网掩码正则表达式代码实例:在实际应用中可能验证子网掩码合法性的需求并不多&#xff0c;但并不能够保证绝对没有&#xff0c;下面就分享一段能够实现此功能的代码实例&#xff0c;希望能够给需要的朋友带来一定的帮助&#xff0c;代码如下:function checkMask(mask){var…

c# 可变二维数组_C#| 具有固定行大小和可变列大小的二维数组

c# 可变二维数组Here, the task is to declare a two dimensional array – where number rows are given and columns are variable length: 在这里&#xff0c;任务是声明一个二维数组-其中给出了行数&#xff0c;列数是可变长度 &#xff1a; To declare such array, we us…

loop和[bx]的联合应用

计算ffff:0-ffff:b单元中的数据的和&#xff0c;结果存储在dx中&#xff08;8086&#xff09; assume cs:codecode segmentmov ax,0ffffhmov ds,axmov bx,0mov dx,0mov cx,12s: mov al,[bx]mov ah,0add dx,axinc bxloop smov ax,4c00hint 21hcode ends endloop相当于for循环&a…

javascript实现网页对联广告

1、将下面代码放到<head></head>之间<SCRIPT languageJavaScript src"tan.js"></SCRIPT> 2、tan.js文件下载地址/Files/MaxIE/2005618154128678.rar转载于:https://www.cnblogs.com/MaxIE/archive/2005/12/22/302475.html

c语言打印数组元素_C程序打印元素差为0或1的子集数

c语言打印数组元素Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1. 给定一个整数数组&#xff0c;查找并…

将数据、代码、栈放入不同的栈(8086)

先上程序&#xff1a; assume cs:code,ds:data,ss:stackdata segmentdw 0123h,0456h,0789h,0abch,0defh,0fedh,0cbah,0987h data endsstack segmentdw 0,0,0,0,0,0,0,0,0,0 stack endscode segmentstart: mov ax,stackmov ss,axmov sp,20hmov ax,datamov ds,axmov bx,0mov cx,…

ftp put 550 Access is denied

ii6 要想让用户上传文件&#xff0c;必须给整个ftp站点赋予写入权限转载于:https://www.cnblogs.com/dataflowjia/archive/2011/04/06/2007173.html

从MapX到MapXtreme2004[2]-图层操作

Mapx中基本的图层操作还是比较简单的&#xff0c;集中在对Layers和Layer的处理上&#xff0c;对别的没有太多要求。在MapXtreme中&#xff0c;要完成类似功能&#xff0c;发生了一点变化&#xff0c;如下&#xff1a;1、图层的显示在MapXtreme中&#xff0c;图层的显示控制发生…

nextcloud+nginx+mysql,Ubuntu搭建Nginx-Nextcloud环境

环境部署与安装安装mysql-server“$ sudo apt-get install mysql-server“安装的过程中会设置密码进入mysql“$ mysql -u root -p“创建数据库名“$ reate database nextcloud“查看数据名称“$ show databases“安装PHP7.0“$ sudo apt-get install php7.0-*“关于PHP7.0相关模…

抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性

抛硬币正面期望Problem statement: 问题陈述&#xff1a; Let N be a positive odd number. There are N coins, numbered 1, 2 , ... , N. For each i (1≤i≤N), when Coin i is tossed, it comes up heads with probability pi and tails with probability 1-pi. 令N为正奇…

委托用法

一、委托callback 回调函数声明委托&#xff1a;&#xff08;与声明类相似&#xff09;public delegate string MyDelegate(string sInput);使用委托&#xff1a;1. MyDelegate myDelegateCase new MyDelegate(InvokeMethod);private string InvokeMethod(string sInput){retu…

汇编程序中,字符数据和ASCII的对应关系

在汇编程序中&#xff0c;用’…的方式指明数据是以字符的形式给出的&#xff0c;编译器将它们转化为对应的ASCII码。 assume cs:code,ds:datadata segmentdb unIXdb foRK data endscode segmentstart: mov al,amov bl,bmov ax,4c00hint 21h code ends end startdb ‘unIX’ 相…