在此后搜索中,讨论了排序数组中的插入和删除操作。
搜索操作
在排序的数组中,可以通过使用二进制搜索来执行搜索操作。
C++
// C++ program to implement binary search in sorted array
#include
using namespace std;
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Driver code */
int main()
{
// Let us search 3 in below array
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof(arr) / sizeof(arr[0]);
key = 10;
cout << "Index: " << binarySearch(arr, 0, n - 1, key)
<< endl;
return 0;
}
// This code is contributed by NamrataSrivastava1
C
// C program to implement binary search in sorted array
#include
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2; /*low + (high - low)/2;*/
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Driver Code */
int main()
{
// Let us search 3 in below array
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof(arr) / sizeof(arr[0]);
key = 10;
printf("Index: %d\n", binarySearch(arr, 0, n-1, key));
return 0;
}
Java
// Java program to implement binary
// search in a sorted array
class Main {
// function to implement
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
/*low + (high - low)/2;*/
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Driver Code*/
public static void main(String[] args)
{
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = arr.length - 1;
key = 10;
System.out.println("Index: " + binarySearch(arr, 0, n, key));
}
}
Python3
# python 3 program to implement
# binary search in sorted array
def binarySearch(arr, low, high, key):
if (high < low):
return -1
# low + (high - low)/2
mid = (low + high)/2
if (key == arr[int(mid)]):
return mid
if (key > arr[int(mid)]):
return binarySearch(arr,
(mid + 1), high, key)
return (binarySearch(arr, low,
(mid -1), key))
# Driver program to check above functions
# Let us search 3 in below array
arr = [5, 6, 7, 8, 9, 10]
n = len(arr)
key = 10
print("Index:", int(binarySearch(arr, 0, n-1, key) ))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to implement binary
// search in a sorted array
using System;
public class GFG {
// function to implement
// binary search
public static int binarySearch(int[] arr, int low,
int high, int key)
{
if (high < low) {
return -1;
}
/*low + (high - low)/2;*/
int mid = (low + high) / 2;
if (key == arr[mid]) {
return mid;
}
if (key > arr[mid]) {
return binarySearch(arr, (mid + 1), high, key);
}
return binarySearch(arr, low, (mid - 1), key);
}
/* Driver Code */
public static void Main(string[] args)
{
int[] arr = new int[] { 5, 6, 7, 8, 9, 10 };
int n, key;
n = arr.Length;
key = 10;
Console.WriteLine("Index: "
+ binarySearch(arr, 0, n-1, key));
}
}
// This code is contributed by Shrikant13
PHP
// PHP program to implement
// binary search in sorted array
$arr[(int)$mid])
return binarySearch($arr, ($mid + 1),
$high, $key);
return (binarySearch($arr, $low,
($mid -1), $key));
}
// Driver Code
// Let us search 3 in below array
$arr = array(5, 6, 7, 8, 9, 10);
$n = count($arr);
$key = 10;
echo "Index: ", (int)binarySearch($arr, 0,
$n-1, $key);
// This code is contributed by
// Srathore
?>
Javascript
C++
// C++ program to implement insert operation in
// an sorted array.
#include
using namespace std;
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver code */
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
cout<< "\nBefore Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
// Inserting key
n = insertSorted(arr, n, key, capacity);
cout << "\nAfter Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i]<< " ";
return 0;
}
// This code is contributed by SHUBHAMSINGH10
C
// C program to implement insert operation in
// an sorted array.
#include
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
printf("\nBefore Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
printf("\nAfter Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java program to insert an
// element in a sorted array
class Main {
// Inserts a key in arr[] of given
// capacity. n is current size of arr[].
// This function returns n+1 if insertion
// is successful, else n.
static int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = arr.length;
int n = 6;
int key = 26;
System.out.print("\nBefore Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Inserting key
n = insertSorted(arr, n, key, capacity);
System.out.print("\nAfter Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python3
# Python3 program to implement insert
# operation in an sorted array.
# Inserts a key in arr[] of given capacity.
# n is current size of arr[]. This function
# returns n+1 if insertion is successful, else n.
def insertSorted(arr, n, key, capacity):
# Cannot insert more elements if n is
# already more than or equal to capcity
if (n >= capacity):
return n
i = n - 1
while i >= 0 and arr[i] > key:
arr[i + 1] = arr[i]
i -= 1
arr[i + 1] = key
return (n + 1)
# Driver Code
arr = [12, 16, 20, 40, 50, 70]
for i in range(20):
arr.append(0)
capacity = len(arr)
n = 6
key = 26
print("Before Insertion: ", end = " ");
for i in range(n):
print(arr[i], end = " ")
# Inserting key
n = insertSorted(arr, n, key, capacity)
print("\nAfter Insertion: ", end = "")
for i in range(n):
print(arr[i], end = " ")
# This code is contributed by Mohit Kumar
C#
using System;
// C# program to insert an
// element in a sorted array
public class GFG {
// Inserts a key in arr[] of given
// capacity. n is current size of arr[].
// This function returns n+1 if insertion
// is successful, else n.
public static int insertSorted(int[] arr, int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity) {
return n;
}
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--) {
arr[i + 1] = arr[i];
}
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
public static void Main(string[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = arr.Length;
int n = 6;
int key = 26;
Console.Write("\nBefore Insertion: ");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
// Inserting key
n = insertSorted(arr, n, key, capacity);
Console.Write("\nAfter Insertion: ");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by Shrikant13
C++
// C++ program to implement delete operation in a
// sorted array
#include
using namespace std;
// To search a ley to be deleted
int binarySearch(int arr[], int low, int high, int key);
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1)
{
cout << "Element not found";
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
// Driver code
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
cout << "Array before deletion\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
}
// This code is contributed by shubhamsingh10
C
// C program to implement delete operation in a
// sorted array
#include
// To search a ley to be deleted
int binarySearch(int arr[], int low, int high, int key);
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
// Driver code
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
printf("Array before deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
n = deleteElement(arr, n, key);
printf("\n\nArray after deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
Java
// Java program to delete an
// element from a sorted array
class Main {
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Function to delete an element */
static int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
/* Driver Code */
public static void main(String[] args)
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = arr.length;
int key = 30;
System.out.print("Array before deletion:\n");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
n = deleteElement(arr, n, key);
System.out.print("\n\nArray after deletion:\n");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python3
# Python program to implement delete operation in a
# sorted array
#/* Function to delete an element */
def deleteElement(arr, n, key):
# Find position of element to be deleted
pos = binarySearch(arr, 0, n - 1, key)
if (pos == -1):
print("Element not found")
return n
# Deleting element
for i in range(pos,n - 1):
arr[i] = arr[i + 1]
return n - 1
# To search a ley to be deleted
def binarySearch(arr, low, high, key):
if (high < low):
return -1
mid = (low + high) // 2
if (key == arr[mid]):
return mid
if (key > arr[mid]):
return binarySearch(arr, (mid + 1), high, key)
return binarySearch(arr, low, (mid - 1), key)
# Driver code
arr = [10, 20, 30, 40, 50 ]
n = len(arr)
key = 30
print("Array before deletion")
for i in range(n):
print(arr[i],end=" ")
n = deleteElement(arr, n, key)
print("\n\nArray after deletion")
for i in range(n):
print(arr[i],end=" ")
# This code is contributed by shubhamsingh10
C#
// C# program to delete an
// element from a sorted array
using System;
public class GFG {
// binary search
static int binarySearch(int[] arr, int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Function to delete an element */
static int deleteElement(int[] arr, int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
Console.WriteLine("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
/* Driver Code */
public static void Main()
{
int i;
int[] arr = { 10, 20, 30, 40, 50 };
int n = arr.Length;
int key = 30;
Console.Write("Array before deletion:\n");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
n = deleteElement(arr, n, key);
Console.Write("\n\nArray after deletion:\n");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Rajput-Ji
输出
Index: 5
搜索操作的时间复杂度: O(Log n)[使用二进制搜索]
插入操作
在未排序的数组中,插入操作比已排序的数组要快,因为我们不必关心要放置元素的位置。
C++
// C++ program to implement insert operation in
// an sorted array.
#include
using namespace std;
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver code */
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
cout<< "\nBefore Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
// Inserting key
n = insertSorted(arr, n, key, capacity);
cout << "\nAfter Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i]<< " ";
return 0;
}
// This code is contributed by SHUBHAMSINGH10
C
// C program to implement insert operation in
// an sorted array.
#include
// Inserts a key in arr[] of given capacity. n is current
// size of arr[]. This function returns n+1 if insertion
// is successful, else n.
int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
printf("\nBefore Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
printf("\nAfter Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java program to insert an
// element in a sorted array
class Main {
// Inserts a key in arr[] of given
// capacity. n is current size of arr[].
// This function returns n+1 if insertion
// is successful, else n.
static int insertSorted(int arr[], int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity)
return n;
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = arr.length;
int n = 6;
int key = 26;
System.out.print("\nBefore Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Inserting key
n = insertSorted(arr, n, key, capacity);
System.out.print("\nAfter Insertion: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python3
# Python3 program to implement insert
# operation in an sorted array.
# Inserts a key in arr[] of given capacity.
# n is current size of arr[]. This function
# returns n+1 if insertion is successful, else n.
def insertSorted(arr, n, key, capacity):
# Cannot insert more elements if n is
# already more than or equal to capcity
if (n >= capacity):
return n
i = n - 1
while i >= 0 and arr[i] > key:
arr[i + 1] = arr[i]
i -= 1
arr[i + 1] = key
return (n + 1)
# Driver Code
arr = [12, 16, 20, 40, 50, 70]
for i in range(20):
arr.append(0)
capacity = len(arr)
n = 6
key = 26
print("Before Insertion: ", end = " ");
for i in range(n):
print(arr[i], end = " ")
# Inserting key
n = insertSorted(arr, n, key, capacity)
print("\nAfter Insertion: ", end = "")
for i in range(n):
print(arr[i], end = " ")
# This code is contributed by Mohit Kumar
C#
using System;
// C# program to insert an
// element in a sorted array
public class GFG {
// Inserts a key in arr[] of given
// capacity. n is current size of arr[].
// This function returns n+1 if insertion
// is successful, else n.
public static int insertSorted(int[] arr, int n, int key, int capacity)
{
// Cannot insert more elements if n is already
// more than or equal to capcity
if (n >= capacity) {
return n;
}
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--) {
arr[i + 1] = arr[i];
}
arr[i + 1] = key;
return (n + 1);
}
/* Driver program to test above function */
public static void Main(string[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = arr.Length;
int n = 6;
int key = 26;
Console.Write("\nBefore Insertion: ");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
// Inserting key
n = insertSorted(arr, n, key, capacity);
Console.Write("\nAfter Insertion: ");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
// This code is contributed by Shrikant13
输出
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 26 40 50 70
插入操作的时间复杂度: O(n)[在最坏的情况下,所有元素都可能需要移动]
删除操作
在删除操作中,使用二进制搜索来搜索要删除的元素,然后执行删除操作,然后移动这些元素。
C++
// C++ program to implement delete operation in a
// sorted array
#include
using namespace std;
// To search a ley to be deleted
int binarySearch(int arr[], int low, int high, int key);
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1)
{
cout << "Element not found";
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
// Driver code
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
cout << "Array before deletion\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
}
// This code is contributed by shubhamsingh10
C
// C program to implement delete operation in a
// sorted array
#include
// To search a ley to be deleted
int binarySearch(int arr[], int low, int high, int key);
/* Function to delete an element */
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
// Driver code
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
printf("Array before deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
n = deleteElement(arr, n, key);
printf("\n\nArray after deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
Java
// Java program to delete an
// element from a sorted array
class Main {
// binary search
static int binarySearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Function to delete an element */
static int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
/* Driver Code */
public static void main(String[] args)
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = arr.length;
int key = 30;
System.out.print("Array before deletion:\n");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
n = deleteElement(arr, n, key);
System.out.print("\n\nArray after deletion:\n");
for (i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python3
# Python program to implement delete operation in a
# sorted array
#/* Function to delete an element */
def deleteElement(arr, n, key):
# Find position of element to be deleted
pos = binarySearch(arr, 0, n - 1, key)
if (pos == -1):
print("Element not found")
return n
# Deleting element
for i in range(pos,n - 1):
arr[i] = arr[i + 1]
return n - 1
# To search a ley to be deleted
def binarySearch(arr, low, high, key):
if (high < low):
return -1
mid = (low + high) // 2
if (key == arr[mid]):
return mid
if (key > arr[mid]):
return binarySearch(arr, (mid + 1), high, key)
return binarySearch(arr, low, (mid - 1), key)
# Driver code
arr = [10, 20, 30, 40, 50 ]
n = len(arr)
key = 30
print("Array before deletion")
for i in range(n):
print(arr[i],end=" ")
n = deleteElement(arr, n, key)
print("\n\nArray after deletion")
for i in range(n):
print(arr[i],end=" ")
# This code is contributed by shubhamsingh10
C#
// C# program to delete an
// element from a sorted array
using System;
public class GFG {
// binary search
static int binarySearch(int[] arr, int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
/* Function to delete an element */
static int deleteElement(int[] arr, int n, int key)
{
// Find position of element to be deleted
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
Console.WriteLine("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
/* Driver Code */
public static void Main()
{
int i;
int[] arr = { 10, 20, 30, 40, 50 };
int n = arr.Length;
int key = 30;
Console.Write("Array before deletion:\n");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
n = deleteElement(arr, n, key);
Console.Write("\n\nArray after deletion:\n");
for (i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Rajput-Ji
输出
Array before deletion
10 20 30 40 50
Array after deletion
10 20 40 50
删除操作的时间复杂度: O(n)[在最坏的情况下,所有元素都可能必须移动]
?list = PLqM7alHXFySEQDk2MDfbwEdjd2svVJH9p