Home php教程 php手册 ThinkPHP整合支付宝担保交易

ThinkPHP整合支付宝担保交易

Jun 07, 2016 am 11:36 AM

ThinkPHP整合支付宝担保交易
本代码参考大神 http://www.thinkphp.cn/code/240.html 的思路

1.登陆支付宝后台,下载担保交易的集成包。


2.下载完成后的文件说明:

纯担保交易接口-create_partner_trade_by_buyer(20151015)
确认发货接口-send_goods_confirm_by_platform(20150312)
根据自己需要去选择,需要说明下,先担保整合完成后才回去处理确认发货,因为确认发货时需要担保交易的支付宝交易编号

对应的代码文件结构───────<br> create_partner_trade_by_buyer-php-UTF-8<br>   │<br>   ├lib┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈类文件夹<br>   │  │<br>   │  ├alipay_core.function.php ┈┈┈┈┈┈支付宝接口公用函数文件<br>   │  │<br>   │  ├alipay_notify.class.php┈┈┈┈┈┈┈支付宝通知处理类文件<br>   │  │<br>   │  ├alipay_submit.class.php┈┈┈┈┈┈┈支付宝各接口请求提交类文件<br>   │  │<br>   │  └alipay_md5.function.php┈┈┈┈┈┈┈支付宝接口MD5函数文件<br>   │<br>   ├log.txt┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈日志文件<br>   │<br>   ├alipay.config.php┈┈┈┈┈┈┈┈┈┈┈┈基础配置类文件<br>   │<br>   ├alipayapi.php┈┈┈┈┈┈┈┈┈┈┈┈┈┈支付宝接口入口文件<br>   │<br>   ├notify_url.php ┈┈┈┈┈┈┈┈┈┈┈┈┈服务器异步通知页面文件<br>   │<br>   ├return_url.php ┈┈┈┈┈┈┈┈┈┈┈┈┈页面跳转同步通知文件<br>   │<br>   ├cacert.pem ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈用于CURL中校验SSL的CA证书文件<br>   │<br>   └readme.txt ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈使用说明文本这里我们先处理代码

3.核心处理代码

把lib目录文件下的4个核心文件放入 ThinkPHP/Library/Vendor/Alipay 下

修改文件名为:alipay_core.function.php       ->        Corefunction.php<br> alipay_notify.class.php        ->        Notify.php<br> alipay_submit.class.php        ->        Submit.php<br> alipay_md5.function.php        ->        Md5function.php<br> 其中 Notify.php 和 Md5function.php 需要删除前面引入的两行代码require_once("alipay_core.function.php");
require_once("alipay_md5.function.php");
因为在使用TP第三方扩展类的时候会自动引入他需要的这两个文件

核心框架整合完成之后我们来整理逻辑代码。

4.逻辑代码整理

首先我们把 签名文件 cacert.pem 放在网站的跟目录,其他目录也行,不过需要有访问权限的

然后在公共配置文件conf.php中添加 支付宝配置    //支付宝配置参数<br> 'alipay_config'=>array(<br>     'partner' =>'2088**********************',   //这里是你在成功申请支付宝接口后获取到的PID;<br>     'key'=>'ob4x7k0*************************',//这里是你在成功申请支付宝接口后获取到的Key<br>     'sign_type'=>strtoupper('MD5'),<br>     'input_charset'=> strtolower('utf-8'),<br>     'cacert'=> getcwd().'\\cacert.pem',//liunx这里需要注意 \\ 和 / 在liunx的区别<br>     'transport'=> 'http',<br>     'seller_email'=>'775919499@qq.com',// 这里是你的收款账号,<br> ),<br> //以上配置项,是从接口包中alipay.config.php 文件中复制过来,进行配置;<br>     'alipay'   =>array(<br> //这里是异步通知页面url,提交到项目的Pay控制器的notifyurl方法;<br> 'notify_url'=>'http://www.loveteemo.com/Pay/notifyurl', <br> //这里是页面跳转通知url,提交到项目的Pay控制器的returnurl方法;<br> 'return_url'=>'http://www.loveteemo.com/Pay/returnurl',<br> ),然后去创建一个 Key 控制器,然后处理代码:<?php <br /> namespace Home\Controller;<br> use Think\Controller;<br> class KeyController extends Controller {<br>     //利用构造函数引入核心文件<br>     public function _initialize() {<br>         vendor('Alipay.Corefunction');<br>         vendor('Alipay.Md5function');<br>         vendor('Alipay.Notify');<br>         vendor('Alipay.Submit');    <br>     }<br>   //首页,用来展示商品页<br>     public function index(){<br>         $this->display();<br>     }<br>     //订单页,我写死了的,可以根据自己需要进行修改<br>     public function order(){<br>         $type = I('get.type');<br>         if($type==1){<br>         $date = array('type'=>1,"price"=>'0.01',"name"=>"《火星救援》");<br>         }elseif($type==2){<br>         $date = array('type'=>2,"price"=>'0.01',"name"=>"《死神的精确度》");<br>         }elseif($type==3){<br>         $date = array('type'=>3,"price"=>'0.01',"name"=>"《寂寞是毒,也是解药》");<br>         }elseif($type==4){<br>         $date = array('type'=>4,"price"=>'0.01',"name"=>"《只要不忘了回家的路》");<br>         }elseif($type==5){<br>         $date = array('type'=>5,"price"=>'0.01',"name"=>"《异想星球 hello,我是托比小黑》");<br>         }elseif($type==6){<br>         $date = array('type'=>6,"price"=>'0.01',"name"=>"《张鸣说历史:大国的虚与实》");<br>         }<br>         $this->time = time();<br>         $this->assign("data",$date);<br>         $this->display();<br>     }<br>     //订单页点击提交,传递必要参数后开始支付,可自行修改<br>     public function payorder(){<br>         //传递数组配置<br>         $alipay_config=C('alipay_config');<br>         /**************************请求参数**************************/<br>             //支付类型<br>         $payment_type = "1";        //必填,不能修改<br>         //服务器异步通知页面路径<br>         $notify_url = C('alipay.notify_url');        //需http://格式的完整路径,不能加?id=123这类自定义参数<br>         //页面跳转同步通知页面路径<br>         $return_url = C('alipay.return_url');         //需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/<br>         //商户订单号<br>         $out_trade_no = $_POST['orderid'];        //商户网站订单系统中唯一订单号,必填<br>         //订单名称<br>         $subject = $_POST['ordername'];        //必填<br>         //付款金额<br>         $price = $_POST['orderprice'];        //必填<br>         //商品数量<br>         $quantity = "1";        //必填,建议默认为1,不改变值,把一次交易看成是一次下订单而非购买一件商品<br>         //物流费用<br>         $logistics_fee = "0.00";        //必填,即运费<br>         //物流类型<br>         $logistics_type = "EXPRESS";        //必填,三个值可选:EXPRESS(快递)、POST(平邮)、EMS(EMS)<br>         //物流支付方式<br>         $logistics_payment = "SELLER_PAY";        //必填,两个值可选:SELLER_PAY(卖家承担运费)、BUYER_PAY(买家承担运费)<br>         //订单描述<br>         $body = $_POST['orderbody'];<br>             //商品展示地址<br>         $show_url = $_POST['ordershow'];        //需以http://开头的完整路径,如:http://www.商户网站.com/myorder.html<br>         //收货人姓名<br>         $receive_name = $_POST['user_name'];        //如:张三<br>         //收货人地址<br>         $receive_address = $_POST['user_address'];        //如:XX省XXX市XXX区XXX路XXX小区XXX栋XXX单元XXX号<br>         //收货人邮编<br>         $receive_zip = $_POST['user_zip'];        //如:123456<br>         //收货人电话号码<br>         $receive_phone = $_POST['user_phone'];        //如:0571-88158090<br>         //收货人手机号码<br>         $receive_mobile = $_POST['user_mobile'];        //如:13312341234<br>         /************************************************************/<br>     <br>         //构造要请求的参数数组,无需改动<br>             $parameter = array(<br>             "service" => "create_partner_trade_by_buyer",<br>             "partner" => trim($alipay_config['partner']),<br>             "seller_email" => trim($alipay_config['seller_email']),<br>             "payment_type"=> $payment_type,<br>             "notify_url"=> $notify_url,<br>             "return_url"=> $return_url,<br>             "out_trade_no"=> $out_trade_no,<br>             "subject"=> $subject,<br>             "price"=> $price,<br>             "quantity"=> $quantity,<br>             "logistics_fee"=> $logistics_fee,<br>             "logistics_type"=> $logistics_type,<br>             "logistics_payment"=> $logistics_payment,<br>             "body"=> $body,<br>             "show_url"=> $show_url,<br>             "receive_name"=> $receive_name,<br>             "receive_address"=> $receive_address,<br>             "receive_zip"=> $receive_zip,<br>             "receive_phone"=> $receive_phone,<br>             "receive_mobile"=> $receive_mobile,<br>             "_input_charset"=> trim(strtolower($alipay_config['input_charset']))<br>             );<br>             //存入数据库订单信息 static为99是无效订单<br>             M('test')->add(array("orderid"=>$out_trade_no,"addtime"=>time(),"ordername"=>$subject,"orderprice"=>$price,"static"=>99));<br>             //建立请求<br>             $alipaySubmit = new \AlipaySubmit($alipay_config);<br>             //dump($alipaySubmit);die;<br>             $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");<br>             echo $html_text;<br>             }<br>     /******************************    服务器异步通知页面方法    *******************************/<br>     public function notifyurl(){<br>         //防止乱码<br>         header("Content-Type:text/html;charset=utf-8");<br>         //计算得出通知验证结果<br>         $alipay_config=C('alipay_config');<br>         $alipayNotify = new \AlipayNotify($alipay_config);<br>         $verify_result = $alipayNotify->verifyNotify();<br>         if($verify_result) {//验证成功<br>         //商户订单号<br>         logResult("订单编号:".$_POST['out_trade_no'].",状态".$_POST['trade_status']."");<br>         $out_trade_no = $_POST['out_trade_no'];<br>         //支付宝交易号<br>         $trade_no = $_POST['trade_no'];<br>         //交易状态<br>         $trade_status = $_POST['trade_status'];<br>         if($_POST['trade_status'] == 'WAIT_BUYER_PAY') {<br>         //该判断表示买家已在支付宝交易管理中产生了交易记录,但没有付款<br>         //判断该笔订单是否在商户网站中已经做过处理<br>         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序<br>         //如果有做过处理,不执行商户的业务程序<br>                 echo "success";//请不要修改或删除<br>                 //调试用,写文本函数记录程序运行情况是否正常<br>                 //获取支付宝的订单号后写入数据库,修改订单状态为0 待支付<br>                M('test')->where(array("orderid"=>$out_trade_no))->save(array("static"=>0,"lasttime"=>time(),'payid'=>$trade_no));<br>                //文本日志文件,这里的日志文件会在网站根目录生成一个log.txt文件<br>                 logResult("这里是等待付款");<br>             }<br>         else if($_POST['trade_status'] == 'WAIT_SELLER_SEND_GOODS') {<br>         //该判断表示买家已在支付宝交易管理中产生了交易记录且付款成功,但卖家没有发货<br>         //判断该笔订单是否在商户网站中已经做过处理<br>         //买家支付后,修改状态为已支付代发货<br>         M('test')->where(array("orderid"=>$out_trade_no))->save(array("static"=>1,"lasttime"=>time()));<br>         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序<br>         //如果有做过处理,不执行商户的业务程序<br>                 echo "success";//请不要修改或删除<br>                 //调试用,写文本函数记录程序运行情况是否正常<br>                 logResult("支付完成!订单编号:".$out_trade_no.",状态".$_POST['trade_status']."");<br>             }<br>         else if($_POST['trade_status'] == 'WAIT_BUYER_CONFIRM_GOODS') {<br>         //该判断表示卖家已经发了货,但买家还没有做确认收货的操作<br>         //判断该笔订单是否在商户网站中已经做过处理<br>         //发货完成后会修改状态<br>         M('test')->where(array("orderid"=>$out_trade_no))->save(array("static"=>2,"lasttime"=>time()));<br>         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序<br>         //如果有做过处理,不执行商户的业务程序<br>                 echo "success";//请不要修改或删除<br>                 //调试用,写文本函数记录程序运行情况是否正常<br>                 logResult("发货完成,等待买家收货");<br>             }<br>         else if($_POST['trade_status'] == 'TRADE_FINISHED') {<br>         //该判断表示买家已经确认收货,这笔交易完成<br>         //判断该笔订单是否在商户网站中已经做过处理<br>         //买家收货后订单完成<br>         M('test')->where(array("orderid"=>$out_trade_no))->save(array("static"=>3,"lasttime"=>time()));<br>         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序<br>         //如果有做过处理,不执行商户的业务程序<br>                 echo "success";//请不要修改或删除<br>                 //调试用,写文本函数记录程序运行情况是否正常<br>                 logResult("交易完成!");<br>             }<br>             else {<br>         //其他状态判断<br>                 echo "success";<br>                 //调试用,写文本函数记录程序运行情况是否正常<br>                 logResult ("错误");<br>             }<br>         }<br>         else {<br>             //验证失败<br>             echo "fail";<br>             //调试用,写文本函数记录程序运行情况是否正常<br>             logResult("验证失败");<br>         }<br>     }<br>     <br>     /*        页面跳转处理方法;        */<br>     public function returnurl(){<br>         $alipay_config=C('alipay_config');<br>          //计算得出通知验证结果<br>         $alipayNotify = new \AlipayNotify($alipay_config);<br>         $verify_result = $alipayNotify->verifyReturn();<br>         if($verify_result) {//验证成功<br>         //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——<br>             //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表<br>         //商户订单号<br>         $out_trade_no = $_GET['out_trade_no'];<br>         //支付宝交易号<br>         $trade_no = $_GET['trade_no'];<br>         //交易状态<br>         $trade_status = $_GET['trade_status'];<br>             if($_GET['trade_status'] == 'WAIT_SELLER_SEND_GOODS') {<br>         //判断该笔订单是否在商户网站中已经做过处理<br>         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序<br>         //如果有做过处理,不执行商户的业务程序<br>             }<br>         //<br>             else {<br>               echo "trade_status=".$_GET['trade_status'];<br>             }<br>         $this->assign("payid",$trade_no);<br>         }<br>         else {<br>             //验证失败<br>             //如要调试,请看alipay_notify.php页面的verifyReturn函数<br>             echo "验证失败";<br>         }<br>         $this->display();<br> }<br> //自动发货 利用隐藏表单传递必须数据过来<br>        public function sendgoods(){<br>             $alipay_config=C('alipay_config');<br>              /**************************请求参数**************************/<br>         //支付宝交易号<br>         $trade_no = $_POST['WIDtrade_no'];<br>         //必填<br>         //物流公司名称<br>         $logistics_name = $_POST['WIDlogistics_name'];<br>         //必填<br>         //物流发货单号<br>         $invoice_no = $_POST['WIDinvoice_no'];<br>         //物流运输类型<br>         $transport_type = $_POST['WIDtransport_type'];<br>         //三个值可选:POST(平邮)、EXPRESS(快递)、EMS(EMS)<br>         /************************************************************/<br>         //构造要请求的参数数组,无需改动<br>         $parameter = array(<br>         "service" => "send_goods_confirm_by_platform",<br>         "partner" => trim($alipay_config['partner']),<br>         "trade_no"=> $trade_no,<br>         "logistics_name"=> $logistics_name,<br>         "invoice_no"=> $invoice_no,<br>         "transport_type"=> $transport_type,<br>         "_input_charset"=> trim(strtolower($alipay_config['input_charset']))<br>         );<br>         //建立请求<br>         $alipaySubmit = new \AlipaySubmit($alipay_config);<br>         $html_text = $alipaySubmit->buildRequestHttp($parameter);<br>         //解析XML<br>         //注意:该功能PHP5环境及以上支持,需开通curl、SSL等PHP配置环境。建议本地调试时使用PHP开发软件<br>         $doc = new \DOMDocument();<br>         $doc->loadXML($html_text);<br>         //请在这里加上商户的业务逻辑程序代码<br>         //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——<br>         //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表<br>         //解析XML<br>         if( ! empty($doc->getElementsByTagName( "alipay" )->item(0)->nodeValue) ) {<br>         $alipay = $doc->getElementsByTagName( "alipay" )->item(0)->nodeValue;<br>         //echo $alipay;<br>         //M('test')->where(array("orderid"=>$out_trade_no))->save(array("static"=>2,"lasttime"=>time()));<br>         $this->success("自动发货完成!","/Home/Key/lists");<br>         }<br> }<br>     //筛选无效订单后展示<br>     public function lists(){<br>         $lists = M('test')->where("static != 99")->limit(20)->order("id desc")->select();<br>         $this->assign("lists",$lists);<br>         $this->display();<br>     }<br> }[code]到这里基本的业务逻辑就完成了,附带测试的数据库给大家分享下[code]CREATE TABLE `web_test` (<br>   `id` int(11) NOT NULL AUTO_INCREMENT,<br>   `orderid` varchar(16) NOT NULL,<br>   `ordername` varchar(128) NOT NULL,<br>   `orderprice` varchar(16) NOT NULL,<br>   `static` int(11) NOT NULL COMMENT '0为未支付,1为已支付未发货,2为发货为确认收,3为确认收,4为取消',<br>   `addtime` int(11) NOT NULL,<br>   `lasttime` int(11) NOT NULL,<br>   `payid` varchar(32) NOT NULL,<br>   PRIMARY KEY (`id`),<br>   UNIQUE KEY `orderid` (`orderid`)<br> ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;5.需要注意的地方:

签名的目录,上面有说过,签名的目录在liunx和windows是有区别的,liunx需要改成  'cacert'=> getcwd().'/cacert.pem',不然会报错说签名没找到



在部署完成后测试的时候会遇到有些浏览器乱码,谷歌正常

这里需要注意的是TP在异步的时候会出现,在urldecode的时候中文出现乱码,所以在这里我在前面加一行代码防止乱码header("Content-Type:text/html;charset=utf-8");本地测试异步中写操作数据库是没任何意义的

因为异步需要服务器上测试才行的。
打个广告
个人博客 青春博客 www.loveteemo.com 欢迎大家来访
原本链接:http://loveteemo.com/article-130.html

AD:真正免费,域名+虚机+企业邮箱=0元

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles