0% found this document useful (0 votes)
2 views6 pages

Array Bubble Sort Implementation

DS_Array implementation bubble sort

Uploaded by

Usman Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Array Bubble Sort Implementation

DS_Array implementation bubble sort

Uploaded by

Usman Khan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array implementation ∙ bubble sort

Usman Ali [Link]/Uthman782

[Link]/in/uthman-khan-linke [Link]/channel/0029VaFBhspCsU9Wh8i9ze1D
Class Declaration:

#include <iostream>
using namespace std;
class array{
private:
int tsize; //tsize ==> total size,
int usize; //usize ==> used size, of the array
int temp;
int *ptr;

public:

Constructor:
This constructor initializes the array object with a given total size ts. It sets the
used size (usize) to 0, as no elements are inserted initially. The ptr pointer is
dynamically allocated memory to hold ts integers.

array(int ts) : tsize(ts){


this->usize = 0;
this->ptr = new int[ts];
}

Show method:

// array show method.. .


void show(){
cout << "Value" << (usize > 1 ? "s " : " ") << "-> ";
for (int i = 0; i < this->usize; i++)
cout<<(!i ? "{ " : ", ")<<ptr[i]<<(i == usize-1?" }":"");
cout << endl;
}
Bubble sort:

// array sorting method ∙ bubble sort.


bool sort(bool asc = true) {
if (asc) {
for (int i = 0; i < this->usize - 1; i++) {
for (int j = 0; j < this->usize - i - 1; j++) {
if (this->ptr[j] > this->ptr[j + 1]) {
this->temp = this->ptr[j];
this->ptr[j] = this->ptr[j + 1];
this->ptr[j + 1] = this->temp;
}
}
}
} else {
for (int i = 0; i < this->usize - 1; i++) {
for (int j = 0; j < this->usize - i - 1; j++) {
if (this->ptr[j] < this->ptr[j + 1]) {
this->temp = this->ptr[j];
this->ptr[j] = this->ptr[j + 1];
this->ptr[j + 1] = this->temp;
}
}
}
}
return true;
}

Swap () method (Not member method):

// swap method.. .
inline void swap(int *a, int *b){
int temp=*a;
*a=*b;
*b=temp;
}
Bubble sort using swap method:

// array sorting method ∙ bubble sort.


bool sort(bool asc = true) {
if (asc) {
for (int i = 0; i < this->usize - 1; i++) {
for (int j = 0; j < this->usize - i - 1; j++) {
if (this->ptr[j] > this->ptr[j + 1]) {
swap(&ptr[j], &ptr[j + 1]);
}
}
}
} else {
for (int i = 0; i < this->usize - 1; i++) {
for (int j = 0; j < this->usize - i - 1; j++) {
if (this->ptr[j] < this->ptr[j + 1]) {
swap(&ptr[j], &ptr[j + 1]);
}
}
}
}
return true;
}

Destructor:

// deallocates the dynamically allocated memory for the array.


~array(){
cout << "Deleting your Array!" << endl;
delete this->ptr;
}
};
Main function:

int main(void){
array arr(10)={ 2, 4, 3, 5, 1, 6, 2, 7, 3, 9 };
[Link]();
return(0);
}

Values-> { 2, 4, 3, 5, 1, 6, 2, 7, 3, 9 }

int main(void){
array arr(10)={2,4,3,5,1,6,2,7,3,9};
[Link]();
[Link]();
[Link]();
return(0);
}

Values-> { 2, 4, 3, 5, 1, 6, 2, 7, 3, 9 }
Values-> { 1, 2, 2, 3, 3, 4, 5, 6, 7, 9 }

int main(void){
array arr(10)={2,4,3,5,1,6,2,7,3,9};
[Link]();
[Link](false);
[Link]();
return(0);
}
Values-> { 2, 4, 3, 5, 1, 6, 2, 7, 3, 9 }
Values-> { 9, 7, 6, 5, 4, 3, 3, 2, 2, 1 }

Thank you

Usman Ali
[Link]/in/uthman-khan-linke

Follow Like 👍

You might also like