在 Java 中对数组进行排序并在数组中插入元素
原文:https://www . geesforgeks . org/sort-a-array-and-insert-a-element-in-array-in-Java/
对数组进行排序可以通过使用内置的排序函数来完成,而对于插入,我们必须创建一个新的数组来完成,因为 Java 中的数组是不可变的。要了解更多关于 Java 排序的信息,请阅读下面的文章:
方法 1:
- Create a new array of size N+1.
- The new array replicates the first array.
- Insert a number at the end of the array.
- Sort arrays.
示例:插入一个元素,然后对数组进行排序。
Java 语言(一种计算机语言,尤用于创建网站)
// Java program to insert an element in
// an array and then sorting it.
// Importing util files
import java.util.*;
public class Gfg {
// Main function
public static void main(String args[]) throws Exception
{
// Given number
int given_number = 1;
// Array
int array[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };
// Creating new array with increased size
int new_array[] = new int[array.length + 1];
// Copying elements from one array to another
for (int i = 0; i < array.length; i++) {
new_array[i] = array[i];
}
// Adding new element
new_array[new_array.length - 1] = given_number;
// Sorting new array
Arrays.sort(new_array);
// print array
for (int i = 0; i < new_array.length; i++)
System.out.print(new_array[i] + " ");
}
}
Output
1 2 3 4 5 6 7 8 9 10
时间复杂度: O(n log n)
进场 2:
- Tidy up the situation.
- Create a new N+1 array.
- Begins traversing the given array and copying elements.
- If the given number is less than or equal to the number in the array, the given number is appended to the new array.
- Copies the remaining elements of a given array into a new array.
示例:插入一个元素,然后对数组进行排序。
Java
// Java program to insert an element
// in an array and then sorting it.
// Importing util files
import java.util.*;
public class Gfg {
// Main function
public static void main(String args[]) throws Exception
{
// Given Number
int given_number = 1;
// Array
int array[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };
// Sort Given array
Arrays.sort(array);
// Creating new array with increased size
int new_array[] = new int[array.length + 1];
// Copying elements from one
// array to another as in approach 2
int i = 0, j = 0;
for (i = 0; i < new_array.length; i++) {
if (given_number <= array[i]) {
new_array[i] = given_number;
break;
}
else
new_array[i] = array[j++];
}
// copy the remaining elements
for (int k = i + 1; k < new_array.length; k++)
new_array[k] = array[j++];
// print new array
for (i = 0; i < new_array.length; i++)
System.out.print(new_array[i] + " ");
}
}
输出
1 2 3 4 5 6 7 8 9 10
时间复杂度: O(n log n)
方法 3:
- Create a collection.
- Start adding all elements in the collection.
- Copies the remaining elements of the given collection into a new array.
示例:插入一个元素,然后对数组进行排序。
Java
// Java program to insert an element
// in an array and then sorting it.
// Importing util files
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class GFG {
public static void main(String[] args)
{
// using wrapper class here for array,
// to convert into object
Integer arr[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };
Set<Integer> sets
= new HashSet<Integer>(Arrays.asList(arr));
sets.add(1);
arr = sets.toArray(arr);
// print the array
System.out.println(Arrays.toString(arr));
}
}
输出
1 2 3 4 5 6 7 8 9 10
时间复杂度: O(n log n)
版权属于:月萌API www.moonapi.com,转载请注明出处