#include<iostream> #include<string> #include<stdio.h> //using namespace std; int
ID: 3776352 • Letter: #
Question
#include<iostream>
#include<string>
#include<stdio.h>
//using namespace std;
int a[] = { 1, 7, 1, 2, 4, 6, 1, 9, 9, 2 };
int size = sizeof(a)/sizeof(a[0]);
void display() {
for (int i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf(" ");
}
void insertionSort() {
for(int i = 1; i < size; i++) {
for(int j = i-1; j >= 0; j--){
int temp = a[j];
printf("%d > %d ? ", a[j], a[j+1]);
if(a[j] > a[j+1]){
a[j] = a[j+1];
a[j+1] = temp;
}
else
break;
}
display();
}
}
void deleteDuplicates() {
for(int i = size-1; i >= 0; i--) {
if(a[i] == a[i-1])
a[i] = 0;
display();
}
}
void removeDuplicateWithShift(){
int index = 0;
for(int i=0;i<size;i++) {
if(a[i]==a[i+1]) {
index++;
a[i]=0;
}
else {
a[i-index] = a[i];
if(i > 0)
a[i] = 0;
}
}
}
int main(){
display();
insertionSort();
display();
deleteDuplicates();
display();
removeDuplicateWithShift();
display();
return 0;
}
C++ Code
signment 1 ogramming Exercise (Array and Simple Sort Algorithm) 1. In an array example program, add a method called removeDuplicate0 that removes duplicates from a previously sorted array without disrupting the order of index. When a duplicate element is found, the discovered element is then replaced by zero For example, if the elements of the array are: 27 11 4 6 1 3 9 2 After the array is sorted, (you may use any simple sorting algorithm for this part) 11 1 2 2 3 4 679 After the duplicates are removed the elements of the array are: 1 0 0 20 3 4 679 What is the Big(O) of removing duplicates operation? 2. Add another method called, removeDuplicateWithshifto that removes the duplicate and shift the element to an empty index For example, if the elements of the array are 27 11 4 61 3 9 2 After the array is sorted, (you may use any simple sorting algorithm for this part) 1 1 1 2 2 3 4 6 7 9 After the duplicates are removed the elements of the array are: 1 2 3 4 67 900 0 hat is the Big(O) of removing duplicates operation?Explanation / Answer
#include<iostream>
#include<string>
#include<stdio.h>
//using namespace std;
int a[] = { 1, 7, 1, 2, 4, 6, 1, 9, 9, 2 };
int size = sizeof(a)/sizeof(a[0]);
void display() {
for (int i = 0; i < size; i++) {
printf("%d ", a[i]);
}
printf(" ");
}
void insertionSort() {
for(int i = 1; i < size; i++) {
for(int j = i-1; j >= 0; j--){
int temp = a[j];
if(a[j] > a[j+1]){
a[j] = a[j+1];
a[j+1] = temp;
}
else
break;
}
}
}
void deleteDuplicates() {
for(int i = size-1; i >= 0; i--) {
if(a[i] == a[i-1])
a[i] = 0;
}
}
void removeDuplicateWithShift(){
int index = 1;
for(int i=0;i<size;i++) {
for(int j=(i+1);j<size;j++) {
if((a[i]^a[j]) ==0) {
a[j] = 0;
}
if((a[j] == 0) && j < size-1){
if(j!=8)
a[j] = a[j+1] == 0? a[j+2]:a[j+1];
a[j+1] =0;
}
}
}
}
int main(){
display();
insertionSort();
printf("After the array sorting ");
display();
deleteDuplicates();
printf("After the duplicates are remove the element of the array are : ");
display();
removeDuplicateWithShift();
printf("Removes the duplicates and shift the element to an empty index ");
display();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.