Skip to content

Commit 7d2c4e6

Browse files
committed
sort methods and leetcode update
1 parent e3e9f2b commit 7d2c4e6

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

AllKindofSort.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <iostream>
2+
#include <iterator>
3+
#include <vector>
4+
#include <stdlib.h>
5+
#include <cstdlib>
6+
7+
8+
9+
// selection srot
10+
void selectionSort(int unsort[], int n){
11+
for(int i = 0; i < n; i ++){
12+
int min = unsort[i];
13+
int min_index = i;// record the position for swapping
14+
for(int j = i; j < n; j++){
15+
if( min > unsort[j]){
16+
min = unsort[j];
17+
min_index = j;
18+
}
19+
}
20+
// exchange the two values of i and min_index
21+
// in order to put the smallest number at front
22+
if(min_index!= i){
23+
int temp = unsort[i];
24+
unsort[i] = unsort[min_index];
25+
unsort[min_index] = temp;
26+
}
27+
}
28+
}
29+
30+
// Time Complexity is O(n^2)
31+
pseudocode:
32+
for i= 0:n;
33+
for j =i:n;
34+
if (A[i] > A[j])
35+
swap( A[i], A[j]);
36+
37+
void BubbleSort(int unsort[], int n){
38+
for(int i = 0; i < n; i ++){// outer looper
39+
for( int j = 0; j < n-1-i; j ++){ // inner loop, the elements before
40+
if( unsort[j] > unsort[j+1]){
41+
// swap unsort[j] and unsort[j+1]
42+
int temp = unsort[j];
43+
unsort[j] = unsort[j+1];
44+
unsort[j+1] = temp;
45+
}
46+
}
47+
}
48+
}
49+
// to be continued
50+
// merge sort //insertion sort // quick sort - in place ??
51+
52+
void InsertionSort(int unsort[], int n){
53+
for(int i = 1; i < n; i ++){ // outter loop is to keep checking each int
54+
int value = unsort[i];
55+
int j = i - 1;
56+
while(j >= 0 && unsort[j] > value){ // compare each int with the formal ordered list and insert into appriproate position
57+
unsort[j+1] = unsort[j];
58+
j --; // j -- stands for iterator from right to left
59+
unsort[j+1] = value;
60+
}
61+
}
62+
}
63+
64+
65+
66+
// mergesort implemented here and make sure about the inputs
67+
// sounds like the input is stupid
68+
69+
void merge(int unsorted[], int first, int mid, int last, int sorted[]){
70+
int i = first, j = mid, k = 0;
71+
while( i < mid && j < last){
72+
if(unsorted[i] < unsorted[j])
73+
sorted[k++] = unsorted[i++]; // sort[k] = unsorted[j]; k ++; j ++;
74+
else
75+
sorted[k++] = unsorted[j++];
76+
}
77+
while( i < mid)
78+
sorted[k++] = unsorted[i++];
79+
while( j < last)
80+
sorted[k++] = unsorted[j++];
81+
82+
for(int m = 0; m < k; m++)
83+
unsorted[first + m] = sorted[m];
84+
}
85+
86+
void Merge_Sort(int unsorted[], int first, int last, int sorted[]){
87+
if( last > first +1){
88+
int mid = (first + last) /2;
89+
Merge_Sort(unsorted, first, mid, sorted);
90+
Merge_Sort(unsorted, mid, last, sorted);
91+
merge(unsorted, first,mid, last, sorted);
92+
}
93+
}
94+
95+
// find partition of the array
96+
int partition(int unsorted[], int low, int high){
97+
int pivot = unsorted[low];
98+
while( low < high){
99+
while( (low < high) && (unsorted[high] > pivot)) high --;
100+
unsorted[low] = unsorted[high];
101+
while( (low < high) && ( unsorted[low] <= pivot)) low ++;
102+
unsorted[high] = unsorted[low];
103+
}
104+
unsorted[low] = pivot;
105+
return low;
106+
}
107+
108+
109+
void quick_sort(int unsorted[], int low, int high){
110+
int p;
111+
if( low < high){
112+
p = partition(unsorted, low, high);
113+
quick_sort(unsorted, low, p - 1);
114+
quick_sort(unsorted, p + 1, high);
115+
}
116+
}
117+
118+
119+
int main(){
120+
int unsorted[6] = {5,6,3,9,7,8};
121+
int sorted[6];
122+
//selectionSort(unsorted,7);
123+
//BubbleSort(unsorted, 7);
124+
//InsertionSort(unsorted,7);
125+
//Merge_Sort(unsorted,0, 6, sorted);
126+
quick_sort(unsorted,0,5);
127+
std::copy(unsorted, unsorted+6, std::ostream_iterator<int> (std::cout, " "));
128+
std::cin.get();
129+
return 0;
130+
}

Valid Palindrome.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//we have two pointers, starts from begin and the other starts from end
2+
// pseudo code as following:
3+
// while( i < j){
4+
// if(!isalnum(S[i]) i ++; continue;
5+
// if(!isalnum(S[j]) j --; continue;
6+
7+
class Solution {
8+
public:
9+
bool isPalindrome(string s) {
10+
if(s.size() == 0) return true;
11+
int i = 0, j = s.size() - 1;
12+
while( i < j){
13+
if(!isalnum(s[i])){
14+
i ++; continue;
15+
}
16+
if(!isalnum(s[j])){
17+
j --; continue;
18+
} // keep i and j at the same level
19+
if(tolower(s[i]) != tolower(s[j]))
20+
return false;
21+
else{
22+
i ++;
23+
j --;
24+
}
25+
}
26+
return true;
27+
}
28+
};

0 commit comments

Comments
 (0)