java对数组进行排序_用Java对数组进行排序所需的最少交换

java对数组进行排序

Problem:

问题:

In this problem, we would have an unordered array with consecutive distinct natural numbers [1,2,3,..n], where n is the size of the array. We have to find the minimum number of swaps required to sort the array in ascending order.

在此问题中,我们将拥有一个具有连续的不同自然数[1,2,3,.. n]的无序数组,其中n是数组的大小。 我们必须找到按升序对数组进行排序所需的最小交换次数

Note: Think and try by yourself before looking to the solution...

注意:在寻求解决方案之前,请自己思考并尝试...

Solution:

解:

This problem can be solved easily by observing the actual position of elements and their current position , the actual position of element in sorted array will be the a[cur]-1 (element-1), by tracking the actual position of element if we come back to the current element then there exist a cycle , then count the size of that cycle , the number of swaps will be cycling size-1, do this for all the cycles and add them together.

通过观察元素的实际位置及其当前位置,可以很容易地解决此问题,如果我们跟踪元素的实际位置,则排序数组中元素的实际位置将为a [cur -1 ] ( element-1 )回到当前元素,然后存在一个循环,然后计算该循环的大小 ,交换次数将为循环size-1 ,对所有循环进行此操作并将它们加在一起。

Example:

例:

Let an array: A =[2, 4, 5, 1, 3]

设一个数组:A = [2,4,5,1,3]

minimum swap required to sort an array in java

There exist two cycles:
Cycle 1: 2 → 4 → 1 → 2
Cycle 2: 5 → 3 → 5

存在两个周期:
周期1:2→4→1→2
周期2:5→3→5

Size of cycle = number of nodes:
Size of cycle 1=3
Size of cycle 2=2

周期大小=节点数:
周期1的大小= 3
周期2 = 2

Number of swaps: (3-1)+(2-1) = 3

掉期数量: (3-1)+(2-1)= 3

Program:

程序:

import java.io.*;
import java.math.*;
import java.util.*;
public class Swap {
static int minimumSwaps(int[] arr) {
int swap=0;
boolean visited[]=new boolean[arr.length];
for(int i=0;i<arr.length;i++){
int j=i,cycle=0;
while(!visited[j]){
visited[j]=true;
j=arr[j]-1;
cycle++;
}
if(cycle!=0)
swap+=cycle-1;
}
return swap;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int res = minimumSwaps(arr);
System.out.println(res);
scanner.close();
}
}

Output

输出量

    4
4 3 2 1
2

翻译自: https://www.includehelp.com/java-programs/minimum-swaps-required-to-sort-an-array.aspx

java对数组进行排序

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

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

相关文章

如何实现查询附近的人?

查询附近的人或者是附近的商家是一个实用且常用的功能,比如微信中“附近的人”或是美团外卖中“附近商家”等,如下图所示: 那它是如何实现的呢?我们本文就一起来看。 我们本文的面试题是,使用 Redis 如何实现查询附近的人? 典型回答 在说如何实现地理位置查询之前,首…

英语笔记:词组句子:0712

Define sth as … 定义为…… Tie sth to sth 制约 Derive from源自于 Descend from 从……承袭下来 Divide from 分开、隔开 Distinguished from 区别于 Related to 与……有联系 Refer to 查阅、涉及 Attach to 附在……上、系在……上 Associate with 与……有联系…

第五六七章(PTA复习)

第五六七章图形界面线程IO图形界面 主要的布局管理器类包括流布局(FlowLayout)、边界布局(BorderLayout)、网格布局(GridLayout)、卡 片 布 局 (CardLayout) 、 网 格 包 布 局(CardBagLayout) 线程 答案&#xff1a;B IO

Eclipse jetty和plugin 的结合使用

Jetty做为一个轻量级的J2EE Web application server&#xff0c;它不仅小巧&#xff0c;而且性能也比较稳定&#xff0c;效率也挺高&#xff0c;现在也越来越得到广泛的应用。特别是eclipse平台集成了Jetty Plugin后&#xff0c;更是对RCP整合Web Server开发提供了极大的方便。…

python布尔运算符_Python中布尔的逻辑和按位NOT运算符

python布尔运算符In python, not is used for Logical NOT operator, and ~ is used for Bitwise NOT. Here, we will see their usages and implementation in Python. 在python中&#xff0c; not用于逻辑NOT运算符&#xff0c;而〜用于按位NOT。 在这里&#xff0c;我们将看…

英语笔记:作文:What elective to choose

What elective to choose 选修课的选择 Nowadays, thereexist a wide range of selective courses in college which are ready for theundergraduates to choose from. These courses make the students’ college lifemore colorful. However, as for the purpose of choosi…

Redis 有哪些数据类型?

Redis 的数据类型可谓是 Redis 的精华所在&#xff0c;同样的数据类型&#xff0c;例如字符串存储不同的值对应的实际存储结构也是不同&#xff0c;当你存储的 int 值是实际的存储结构也是 int&#xff0c;如果是短字符串&#xff08;小于 44 字节&#xff09;实际存储的结构为…

导出/入数据库

导出/入数据库1、以SQL文件的方式1.1导出1.2 导入2、以mdf和ldf数据库文件的方式2.1导出2.1.1 脱机2.1.2 到数据库的数据路径&#xff0c;拷贝出mdf,ldf文件2.1.3 将原数据库设置为online状态即可正常使用2.2导入数据库&#xff08;切记导入之前要先将控制权限打开&#xff09;…

arm中clz指令_JavaScript中带有示例的Math.clz32()方法

arm中clz指令JavaScript | Math.clz32()方法 (JavaScript | Math.clz32() Method) Math.clz32() is a function in math library of JavaScript that is used to find the number of leading zeroes in the 32-bit representation of the number. The method will return the n…

Oracle 创建用户 scott 例

在OracleXE中创建scott用户1、打开SQL*Plus&#xff0c;以 sys用户登录数据库 connect / as sysdba2、依次执行下面命令 --DROP USER scott CASCADE; CREATE USER scott IDENTIFIED BY tiger; GRANT connect,resource TO scott; GRANT CREATE DATABASE LINK, CREATE MATERIALIZ…

第八章Transact-SQL程序设计

第八章Transact-SQL程序设计8.1_变量8.1.1_局部变量8.1.2_全局变量8.2_流程控制语句8.2.1_IF...ELSE语句8.2.2_while循环语句8.1_变量 8.1.1_局部变量 局部变量的声明定义&#xff1a; Declare Variable_name Datatype[, Variable_name Datatype]…--举例&#xff1a; decla…

Redis 如何处理已经过期的数据?

上一篇我们讲了 Redis 内存用完之后的内存淘汰策略,它主要是用来出来异常情况下的数据清理,而本文讲的是 Redis 的键值过期之后的数据处理,讲的是正常情况下的数据清理,但面试者常常会把两个概念搞混,以至于和期望的工作失之交臂。我们本文的职责之一就是帮读者朋友搞清楚…

如何删除多余系统引导项

我们很多人都装过双系统&#xff0c;但是有时候装的当中却不想装了或者装不成功&#xff0c;生成的多余系统引导项怎么删除呢&#xff1f;下面分享下我的经验&#xff1a;win7&#xff08;XP&#xff09;下如何删除多余的系统引导项。关键词&#xff1a;删除多余系统引导项&…

动态规划编程面试_面试的前25大动态编程问题

动态规划编程面试Dynamic programming is one of the most asked paradigms in any product-based company interviews. You can expect DP in online assessments also if you are in touch with any product-based company. For beginner its, of course, a Tough nut to cra…

第九章存储过程

第九章存储过程9.1_游标的使用9.1.1_游标简介及使用流程9.1.2_游标的声明9.1.3_使用游标读取数据9.1.4_举例说明9.2_存储过程9.2.1_存储过程简介9.2.2_存储过程定义及执行9.2.3_重写存储过程9.2.6_删除存储过程9.2.5_举例说明9.1_游标的使用 9.1.1_游标简介及使用流程 使用游…

Oracle笔记:用户、权限及exp/imp数据

--模式&#xff08;方案&#xff09;逻辑概念&#xff1a;一个数据对象的集合&#xff0c;每一个用户--都有一个与之同名的模式&#xff0c;用于存放此用户名下的所有数据对象。select * from user_objectsselect * from dba_users;--创建用户1、给用户创建自己的数据表空间cre…

Redis 内存用完会怎样?

在某些极端情况下,软件为了能正常运行会做一些保护性的措施,比如运行内存超过最大值之后的处理,以及键值过期之后的处理等,都属于此类问题,而专业而全面的回答这些问题恰好是一个工程师所具备的优秀品质。 我们本文的面试题是 Redis 内存用完之后会怎么? 典型回答 Red…

Linux学习十七、正规表达式练习题

情境模拟题一&#xff1a;透过 grep 搜寻特殊字串&#xff0c;并配合数据流重导向来处理大量的文件搜寻问题。目标&#xff1a;正确的使用正规表示法&#xff1b;前提&#xff1a;需要了解数据流重导向&#xff0c;以及透过子命令 $(command) 来处理档名的搜寻&#xff1b; 我们…

ntp symmetric_Python使用示例设置symmetric_difference()方法

ntp symmetric设置symmetric_difference()方法 (Set symmetric_difference() Method) symmetric_difference() method is used to get the list of all elements which are not common in both sets, the method is called with this set (set1) and another set (set2) is sup…

第十章触发器的创建与管理

第十章触发器的创建与管理10.1_触发器简介10.2_触发器的创建、修改、删除10.2.1_触发器的创建10.2.2_触发器的修改10.2.3_触发器的删除10.2.4_触发器的创建、修改、删除举例10.3_instead of触发器10.3.1_instead of触发器简介及举例 有疑问10.4_inserted、deleted表10.5_注意事…