數組:具備相同類型的元素的集合。數組分配在一塊連續的數據空間上,所以存儲效率低,插入和刪除元素效率低,支持隨機訪問,訪問元素效率高。
鏈表(單向鏈表、雙向鏈表、循環鏈表):鏈表是一種物理存儲單元上非連續、非順序的存儲結構,數據元素的邏輯順序是經過鏈表中的指針連接次序實現的。插入,刪除效率高,空間利用率高,不支持隨機訪問,查找元素效率低複製代碼
1.方案一:第一次查找,找出數組中的最小元素,交換至數組第一個位置,第二次查找數組中除了第一個元素,剩下的部分,找出最小元素交換至數組第二個位置,以此類推。。。
2.方案二:第一次查找Array1,找出最小元素,取出來放到一個一開始爲空的數組Array2中,並將Array1中的那個最小元素刪除,Array1長度減1。第二次查找Array1,找出最小元素,取出來放到一個數組Array2中,並將Array1中的那個最小元素刪除。。。。最後Array1長度爲0,Array2就是排序後的數組。複製代碼
object XuanZe{
def main(args:Array[String]){
var myArray = Array(45, 26, 34, 2, 888, 54, 23, 45, 76, 2);
selectSort(myArray);
for ( x <- myArray ) {
println( x )
}
}
def selectSort(array:Array[Int]):Unit={
var smallest_index:Int = 0;
var temp:Int = 0;
for(i <- 0 to (array.length - 2)){
smallest_index = i;
for(j <- (i+1) to (array.length -1)){
if(array(j)<array(smallest_index)){
smallest_index = j;
}
}
if(smallest_index != i){
temp = array(i);
array(i) = array(smallest_index);
array(smallest_index) = temp;
}
}
}
}複製代碼
import java.util.Arrays;
public class MyClass {
public static void main(String[] args){
int[] sortArray = {45, 26, 34, 2, 888, 54, 23, 45, 76, 2};
selectSort(sortArray);
System.out.println(Arrays.toString(sortArray));
}
private static void selectSort(int[] array){
int smallest_index = 0;
int temp = 0;
for(int i = 0;i<array.length-1;i++){
//無序區的最小數據數組下標
smallest_index = i;
for(int j = i+1;j<array.length;j++){
//在無序區中找到最小數據並保存其數組下標
if(array[j]<array[smallest_index]){
smallest_index = j;
}
}
//若是不是無序區的最小值位置不是默認的第一個數據,則交換之。
if(smallest_index != i){
temp = array[i];
array[i] = array[smallest_index];
array[smallest_index] = temp;
}
}
}
}複製代碼
主文件javascript
#import <Foundation/Foundation.h>
#import "XuanZe.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
XuanZe *xz = [[XuanZe alloc] init];
NSMutableArray* array = (NSMutableArray*)@[@45, @26, @34, @2, @888, @54, @23, @45, @76, @2];
NSLog(@"%@",[xz selectionSort:array]);
}
return 0;
}複製代碼
實現文件java
#import "XuanZe.h"
@implementation XuanZe
-(NSMutableArray*)selectionSort:(NSMutableArray*)array{
int smallest_index = 0;
id temp;
NSMutableArray *arr = [NSMutableArray arrayWithArray:array];
for(int i = 0;i<[arr count];i++){
//無序區的最小數據數組下標
smallest_index = i;
for(int j = i+1;j<[arr count];j++){
//在無序區中找到最小數據並保存其數組下標
if(arr[j]<arr[smallest_index]){
smallest_index = j;
}
}
//若是不是無序區的最小值位置不是默認的第一個數據,則交換之。
if(smallest_index != i){
temp = arr[i];
arr[i] = arr[smallest_index];
arr[smallest_index] = temp;
}
}
return arr;
}
@end複製代碼
def selectionSort(arr):
for i in range(0,len(arr)):
min = i
for j in range(i+1,len(arr)):
if arr[j]<arr[min]:
min = j
arr[i],arr[min] = arr[min],arr[i]
return arr;
print(selectionSort([45,26,34,2,888,54,23,45,76,2]))複製代碼
import scala.collection.mutable.ArrayBuffer
object XuanZe1{
def main(args:Array[String]){
var myArray = ArrayBuffer(45, 26, 34, 2, 888, 54, 23, 45, 76, 2);
var myArray1 = selectSort(myArray);
for ( x <- myArray1 ) {
println( x );
}
}
def selectSort(array:ArrayBuffer[Int]):ArrayBuffer[Int]={
var myArrayBuffer = ArrayBuffer(0,0,0,0,0,0,0,0,0,0);
var myArrayBuffer1 = array;
for(i <- 0 to (array.length - 1)){
var smallest_index:Int = findSmallest(myArrayBuffer1);
myArrayBuffer(i) = myArrayBuffer1(smallest_index);
myArrayBuffer1.remove(smallest_index);
}
return myArrayBuffer;
}
def findSmallest(array:ArrayBuffer[Int]):Int={
var smallest:Int = array(0);
var smallest_index:Int = 0;
for(i <- 1 to (array.length - 1)){
if(array(i)<smallest){
smallest = array(i);
smallest_index = i;
}
}
return smallest_index;
}
}複製代碼
import java.util.Arrays;
public class XuanZe {
public static void main(String[] args){
int[] sortArray = {45, 26, 34, 2, 888, 54, 23, 45, 76, 2};
System.out.println("排序結果"+Arrays.toString(selectionSort(sortArray)));
}
private static int[] selectionSort(int[] array){
int[] newArr = new int[10];
int[] array1 = array;
for(int i = 0;i<array.length;i++){
int smallest_index = finSmallest(array1);
newArr[i] = array1[smallest_index];
//去除最小值,數組縮容
array1[smallest_index] = array1[array1.length-1];
array1 = Arrays.copyOf(array1,array1.length-1);
System.out.println(Arrays.toString(array1));
}
return newArr;
}
private static int finSmallest(int[] arr){
//存儲最小的值
int smallest = arr[0];
//存儲最小值得索引
int smallest_index = 0;
for(int i = 1; i< arr.length;i++){
if(arr[i] < smallest){
smallest = arr[i];
smallest_index = i;
}
}
return smallest_index;
}
}複製代碼
主文件python
#import <Foundation/Foundation.h>
#import "XuanZe.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
XuanZe *xz = [[XuanZe alloc] init];
NSLog(@"%@",[xz selectionSort:(NSMutableArray*)@[@45, @26, @34, @2, @888, @54, @23, @45, @76, @2]]);
}
return 0;
}複製代碼
XuanZe.m文件數組
#import "XuanZe.h"
@implementation XuanZe
-(NSMutableArray*)selectionSort:(NSMutableArray*)array{
NSMutableArray *array1 = [[NSMutableArray alloc] initWithArray:array];
NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:0];
for(int i = 0;i<[array count];i++){
int smallest_index = [self findSmallest:array1];
newArray[i] = array1[smallest_index];
//去除最小值
[array1 removeObjectAtIndex:smallest_index];
}
return newArray;
}
-(int)findSmallest:(NSMutableArray*)array{
//存儲最小的值
int smallest = (int)array[0];
//存儲最小值的索引
int smallest_index = 0;
for(int i = 1; i< [array count];i++){
if((int)array[i] < smallest){
smallest = (int)array[i];
smallest_index = i;
}
}
return smallest_index;
}
@end複製代碼
def findSmallest(arr):
#存儲最小的值
smallest = arr[0]
#存儲最小值的索引
smallest_index = 0
for i in range(1,len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest_index = findSmallest(arr)
#取出數組中最小的元素並將其加入到新的數組中
newArr.append(arr.pop(smallest_index))
return newArr
print(selectionSort([45,26,34,2,888,54,23,45,76,2]))複製代碼