mirror of
https://github.com/wahyd4/wahyd4.github.com.git
synced 2026-08-17 01:25:57 +10:00
367 lines
15 KiB
HTML
367 lines
15 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-Hans">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width">
|
||
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">
|
||
|
||
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
|
||
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
|
||
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
|
||
<link rel="mask-icon" href="/images/logo.svg" color="#222">
|
||
|
||
<link rel="stylesheet" href="/css/main.css">
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
|
||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
|
||
|
||
<script class="next-config" data-name="main" type="application/json">{"hostname":"toozhao.com","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.25.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/config.min.js" defer></script>
|
||
|
||
<meta name="description" content="选择排序大概的思想就是,每一次循环找出需要排序的部分中最小的那个数,找出只有再将最小的那个数移动到它应该放置的那个位置。因为虽然他的遍历次数也是1+2+3+…+n-1,不过它每次循环只交换一次,总的来说效率还是比直接插入排序好一些。更详细的解释参考百度百科和维基百科。 下面上代码吧: package com.toozhao.sort; /** * * @author Junv * */">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:title" content="java实现选择排序">
|
||
<meta property="og:url" content="https://toozhao.com/2012/09/12/java-select-sorting/index.html">
|
||
<meta property="og:site_name" content="大染志">
|
||
<meta property="og:description" content="选择排序大概的思想就是,每一次循环找出需要排序的部分中最小的那个数,找出只有再将最小的那个数移动到它应该放置的那个位置。因为虽然他的遍历次数也是1+2+3+…+n-1,不过它每次循环只交换一次,总的来说效率还是比直接插入排序好一些。更详细的解释参考百度百科和维基百科。 下面上代码吧: package com.toozhao.sort; /** * * @author Junv * */">
|
||
<meta property="og:locale">
|
||
<meta property="article:published_time" content="2012-09-11T14:00:00.000Z">
|
||
<meta property="article:modified_time" content="2025-10-27T10:42:00.923Z">
|
||
<meta property="article:author" content="Junv">
|
||
<meta property="article:tag" content="java">
|
||
<meta property="article:tag" content="算法">
|
||
<meta property="article:tag" content="选择排序">
|
||
<meta name="twitter:card" content="summary">
|
||
|
||
|
||
<link rel="canonical" href="https://toozhao.com/2012/09/12/java-select-sorting/">
|
||
|
||
|
||
<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://toozhao.com/2012/09/12/java-select-sorting/","path":"2012/09/12/java-select-sorting/","title":"java实现选择排序"}</script>
|
||
|
||
<script class="next-config" data-name="calendar" type="application/json">""</script>
|
||
<title>java实现选择排序 | 大染志</title>
|
||
|
||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-7N71TMKK99"></script>
|
||
<script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"G-7N71TMKK99","only_pageview":false,"measure_protocol_api_secret":null}</script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/third-party/analytics/google-analytics.min.js" defer></script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/utils.min.js" defer></script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/motion.min.js" defer></script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/sidebar.min.js" defer></script><script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-theme-next/8.25.0/next-boot.min.js" defer></script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<noscript>
|
||
<link rel="stylesheet" href="/css/noscript.css">
|
||
</noscript>
|
||
</head>
|
||
|
||
<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
|
||
<div class="headband"></div>
|
||
|
||
<main class="main">
|
||
<div class="column">
|
||
<header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
|
||
<div class="site-nav-toggle">
|
||
<div class="toggle" aria-label="Toggle navigation bar" role="button">
|
||
<span class="toggle-line"></span>
|
||
<span class="toggle-line"></span>
|
||
<span class="toggle-line"></span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="site-meta">
|
||
|
||
<a href="/" class="brand" rel="start">
|
||
<i class="logo-line"></i>
|
||
<p class="site-title">大染志</p>
|
||
<i class="logo-line"></i>
|
||
</a>
|
||
<p class="site-subtitle" itemprop="description">想要玩得很 High 的程序员</p>
|
||
</div>
|
||
|
||
<div class="site-nav-right">
|
||
<div class="toggle popup-trigger" aria-label="Search" role="button">
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
|
||
<nav class="site-nav">
|
||
<ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">308</span></a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">249</span></a></li><li class="menu-item menu-item-知识图谱"><a href="https://wiki.toozhao.com/" rel="section" target="_blank"><i class="fa fa-sitemap fa-fw"></i>知识图谱</a></li>
|
||
</ul>
|
||
</nav>
|
||
|
||
|
||
|
||
|
||
</header>
|
||
|
||
|
||
<aside class="sidebar">
|
||
|
||
<div class="sidebar-inner sidebar-overview-active">
|
||
<ul class="sidebar-nav">
|
||
<li class="sidebar-nav-toc">
|
||
Table of Contents
|
||
</li>
|
||
<li class="sidebar-nav-overview">
|
||
Overview
|
||
</li>
|
||
</ul>
|
||
|
||
<div class="sidebar-panel-container">
|
||
<!--noindex-->
|
||
<div class="post-toc-wrap sidebar-panel">
|
||
</div>
|
||
<!--/noindex-->
|
||
|
||
<div class="site-overview-wrap sidebar-panel">
|
||
<div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
|
||
<p class="site-author-name" itemprop="name">Junv</p>
|
||
<div class="site-description" itemprop="description">Go, JavaScript, Ruby on Rails, Java,Docker, CI, CD, Photography</div>
|
||
</div>
|
||
<div class="site-state-wrap animated">
|
||
<nav class="site-state">
|
||
<div class="site-state-item site-state-posts">
|
||
<a href="/archives/">
|
||
<span class="site-state-item-count">308</span>
|
||
<span class="site-state-item-name">posts</span>
|
||
</a>
|
||
</div>
|
||
<div class="site-state-item site-state-categories">
|
||
<a href="/categories/">
|
||
<span class="site-state-item-count">69</span>
|
||
<span class="site-state-item-name">categories</span></a>
|
||
</div>
|
||
<div class="site-state-item site-state-tags">
|
||
<a href="/tags/">
|
||
<span class="site-state-item-count">249</span>
|
||
<span class="site-state-item-name">tags</span></a>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</aside>
|
||
|
||
|
||
</div>
|
||
|
||
<div class="main-inner post posts-expand">
|
||
|
||
|
||
|
||
|
||
|
||
<div class="post-block">
|
||
|
||
|
||
|
||
<article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
|
||
<link itemprop="mainEntityOfPage" href="https://toozhao.com/2012/09/12/java-select-sorting/">
|
||
|
||
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
|
||
<meta itemprop="image" content="/images/avatar.gif">
|
||
<meta itemprop="name" content="Junv">
|
||
</span>
|
||
|
||
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
|
||
<meta itemprop="name" content="大染志">
|
||
<meta itemprop="description" content="Go, JavaScript, Ruby on Rails, Java,Docker, CI, CD, Photography">
|
||
</span>
|
||
|
||
<span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
|
||
<meta itemprop="name" content="java实现选择排序 | 大染志">
|
||
<meta itemprop="description" content="">
|
||
</span>
|
||
<header class="post-header">
|
||
<h1 class="post-title" itemprop="name headline">
|
||
java实现选择排序
|
||
</h1>
|
||
|
||
<div class="post-meta-container">
|
||
<div class="post-meta">
|
||
<span class="post-meta-item">
|
||
<span class="post-meta-item-icon">
|
||
<i class="far fa-calendar"></i>
|
||
</span>
|
||
<span class="post-meta-item-text">Posted on</span>
|
||
|
||
<time title="Created: 2012-09-12 00:00:00" itemprop="dateCreated datePublished" datetime="2012-09-12T00:00:00+10:00">2012-09-12</time>
|
||
</span>
|
||
<span class="post-meta-item">
|
||
<span class="post-meta-item-icon">
|
||
<i class="far fa-calendar-check"></i>
|
||
</span>
|
||
<span class="post-meta-item-text">Edited on</span>
|
||
<time title="Modified: 2025-10-27 21:42:00" itemprop="dateModified" datetime="2025-10-27T21:42:00+11:00">2025-10-27</time>
|
||
</span>
|
||
<span class="post-meta-item">
|
||
<span class="post-meta-item-icon">
|
||
<i class="far fa-folder"></i>
|
||
</span>
|
||
<span class="post-meta-item-text">In</span>
|
||
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
|
||
<a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
|
||
</span>
|
||
</span>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</header>
|
||
|
||
|
||
|
||
|
||
<div class="post-body" itemprop="articleBody"><p>选择排序大概的思想就是,每一次循环找出需要排序的部分中最小的那个数,找出只有再将最小的那个数移动到它应该放置的那个位置。因为虽然他的遍历次数也是1+2+3+…+n-1,不过它每次循环只交换一次,总的来说效率还是比直接插入排序好一些。更详细的解释参考<a target="_blank" rel="noopener" href="http://baike.baidu.com/view/547263.htm">百度百科</a>和<a target="_blank" rel="noopener" href="http://zh.wikipedia.org/zh/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F">维基百科</a>。</p>
|
||
<p>下面上代码吧:</p>
|
||
<pre class="brush: java; title: ; notranslate" title="">package com.toozhao.sort;
|
||
|
||
/**
|
||
*
|
||
* @author Junv
|
||
*
|
||
*/
|
||
public class SelectSort {
|
||
// 定义需要排序的数
|
||
private static int[] array = { 10, 50, 8, 29, 30, 17, 12, 40, 32, 7, 4, 22 };
|
||
|
||
public static void main(String args[]) {
|
||
sort(array);
|
||
for (int flag : array) {
|
||
System.out.print(flag + " ");
|
||
}
|
||
}
|
||
|
||
public static void sort(int[] data) {
|
||
// 外层循环一次,找到i to (data.length-1)这个数组中最小的一个数。
|
||
for (int i = 0; i < (data.length - 1); i++) {
|
||
// temp用来标注值最小的那个数。
|
||
int temp = i;
|
||
for (int j = i + 1; j < data.length; j++) {
|
||
// 将temp 始终标记为最小那个数。
|
||
if (data[j] < data[temp]) {
|
||
temp = j;
|
||
}
|
||
}
|
||
// 如果i != temp 说明,最小那个数为data[temp],则交换。
|
||
if (i != temp) {
|
||
int t = data[i];
|
||
data[i] = data[temp];
|
||
data[temp] = t;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
</pre>
|
||
|
||
<p>如需下载完整代码,以及其他排序代码,请到:</p>
|
||
<blockquote>
|
||
<pre>github:https://github.com/wahyd4/java-sorting</pre>
|
||
</blockquote>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
|
||
|
||
<footer class="post-footer">
|
||
<div class="post-tags">
|
||
<a href="/tags/java/" rel="tag"># java</a>
|
||
<a href="/tags/%E7%AE%97%E6%B3%95/" rel="tag"># 算法</a>
|
||
<a href="/tags/%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F/" rel="tag"># 选择排序</a>
|
||
</div>
|
||
|
||
|
||
|
||
<div class="post-nav">
|
||
<div class="post-nav-item">
|
||
<a href="/2012/09/11/java-insertion-sort/" rel="prev" title="Java 实现直接插入排序">
|
||
<i class="fa fa-angle-left"></i> Java 实现直接插入排序
|
||
</a>
|
||
</div>
|
||
<div class="post-nav-item">
|
||
<a href="/2012/09/16/huawei-hire-exam/" rel="next" title="华为一个字符串处理的上机题">
|
||
华为一个字符串处理的上机题 <i class="fa fa-angle-right"></i>
|
||
</a>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
</article>
|
||
</div>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</div>
|
||
</main>
|
||
|
||
<footer class="footer">
|
||
<div class="footer-inner">
|
||
|
||
<div class="copyright">
|
||
©
|
||
<span itemprop="copyrightYear">2025</span>
|
||
<span class="with-love">
|
||
<i class="fa fa-heart"></i>
|
||
</span>
|
||
<span class="author" itemprop="copyrightHolder">Junv</span>
|
||
</div>
|
||
<div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a>
|
||
</div>
|
||
|
||
</div>
|
||
</footer>
|
||
|
||
|
||
<div class="toggle sidebar-toggle" role="button">
|
||
<span class="toggle-line"></span>
|
||
<span class="toggle-line"></span>
|
||
<span class="toggle-line"></span>
|
||
</div>
|
||
<div class="sidebar-dimmer"></div>
|
||
<div class="back-to-top" role="button" aria-label="Back to top">
|
||
<i class="fa fa-arrow-up fa-lg"></i>
|
||
<span>0%</span>
|
||
</div>
|
||
<div class="reading-progress-bar"></div>
|
||
|
||
<noscript>
|
||
<div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
|
||
</noscript>
|
||
|
||
</body>
|
||
</html>
|