use a bubble to sort the array and then remove the item with the array sorted. C
ID: 3911680 • Letter: U
Question
use a bubble to sort the array and then remove the item with the array sorted. Create a method for the bubblesort. In Java language
2. Write a method, remove, that takes three parameters: an array of integers the length of the array, and an integer, say, removeltem. The method should find and delete the first occurrence of removeltem in the array, If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Explanation / Answer
Array.java
public class Array {
public Array() {
}
public void removeFirstOccuranceElement(int a[],int size,int val)
{
int flag=0,temp;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for(int i=0;i<size;i++)
{
if(a[i]==val && flag==0)
{
for(int k=i;k<size-1;k++)
{
a[k]=a[k+1];
}
size--;
flag=1;
}
}
if(a.length==0)
{
System.out.println("Array is empty");
}
else if(flag==0)
{
System.out.println("Element "+val+" not found in the array");
}
else if(flag==1)
{
System.out.println("Element "+val+" Deleted");
System.out.println("Displaying Array Elements :");
for(int i=0;i<size;i++)
{
System.out.print(a[i]+" ");
}
System.out.println(" Array Size :"+size);
}
}
}
_________________
Test.java
public class Test {
public static void main(String[] args) {
int arr[]={22,33,44,55,66,77,88,11,22,33};
Array a=new Array();
a.removeFirstOccuranceElement(arr, arr.length,22);
}
}
_____________
Output::
Element 22 Deleted
Displaying Array Elements :
11 22 33 33 44 55 66 77 88
Array Size :9
__________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.